使用Spring的注解方式注入Struts所管理的action

来源:互联网 发布:淘宝优惠券设置 编辑:程序博客网 时间:2024/04/30 11:10

因为集成SSH框架时,常容易出错,所以一般要加上log4j日志文件,来显示错误信息(log4j.properties放在源文件目录下,log4j.jar放在WEB-INF/lib目录下)

首先要加载Spring,一般有三种加载方式:

一.插件方式,struts启动时启动spring

struts配置文件struts-config.xml中增加如下配置:

<plug-in   className="org.springframework.web.struts.ContextLoaderPlugIn">

      <set-property property="contextConfigLocation"

        value="classpath*:applicationContext*.xml" />

</plug-in>

 

二.Servelt方式或者Listener,在web容器启动时启动

在web.xml中:

<!-- 配置全局参数,用Servlet或者Listener  启动要用到 -->

<context-param>

   <param-name>contextConfigLocation</param-name>

   <param-value>classpath*:applicationContext*.xml</param-value>

</context-param>

 

<!-- 通过Listener启动Spring -->

<listener>      

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

 

<!-- 通过Servlet启动Spring -->

<servlet>

   <servlet-name>startspring</servlet-name>   <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>

   <load-on-startup>0</load-on-startup>

</servlet>

 

然后需要在struts-config.xml中配置(这里要注意代码的顺序):

<!-- 将action交给spring管理 -->
 <controller>
  <set-property property="processorClass"
   value="org.springframework.web.struts.DelegatingRequestProcessor" />
 </controller>
 <!-- end -->

 

然后再applicationContext中配置:

<?xml version="1.0" encoding="UTF-8"?>

<!-- 首先要导入相关命名空间 -->
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
 <!-- 开启切面代码 -->
 <aop:aspectj-autoproxy />
 <!-- end -->

 <!-- 通过注解方式自动扫描需交给Sping管理的Bean -->
 <context:component-scan base-package="com.aptech.jb.epet.dao" />
 <context:component-scan base-package="com.aptech.jb.epet.biz" />
 <context:component-scan
  base-package="com.aptech.jb.epet.aop.aspects" />
 <!-- end -->

 <!-- 将hibernate数据源注入sessionFactory -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation">
   <value>classpath:hibernate.cfg.xml</value>
  </property>
 </bean>
 <!-- end -->

 <!-- 将sessionFactory注入Spring事务管理类 -->
 <bean id="txManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <!-- end -->
 
 <!-- 管理Struts的action -->
 <bean name="/Login" class="com.aptech.jb.epet.web.action.LoginAction" />
 <bean name="/PetInfo" class="com.aptech.jb.epet.web.action.PetInfoAction" />
 <bean name="/EditPetInfo" class="com.aptech.jb.epet.web.action.EditPetInfoAction" />
 <bean name="/AllPets" class="com.aptech.jb.epet.web.action.AllPetsAction" />
 <bean name="/Adopt" class="com.aptech.jb.epet.web.action.AdoptAction" />
 <bean name="/Training" class="com.aptech.jb.epet.web.action.TrainingAction" />
 <bean name="/AddDiary" class="com.aptech.jb.epet.web.action.AddDiaryAction" />

 <!-- end -->

 <!-- 开启Sping注释管理事务功能,指定事务管理的Bean -->
 <tx:annotation-driven transaction-manager="txManager" />
 <!-- end -->
</beans>

这里要注意bean里的name值要和struts里的path 相同

 

最后在相关action中注入依赖的biz,这里采用注解的方式

 

public class LoginAction extends Action {
 @Resource
 private PetInfoBiz petInfoBiz;

 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  ..........省略

}

OK!

 

原创粉丝点击