使用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!
- 使用Spring的注解方式注入Struts所管理的action
- spring的注解注入方式
- Spring中使用注解的方式注入方式1
- struts的action注解
- 关于用spring来管理Struts中的action 所出现的问题
- Spring的注解注入方式@Autowired,@Resource
- 使用spring注解方式实现组件的声明,并且用注解注入方式解决依赖关系
- Spring的bean管理(注解注入属性和注解文件混合使用)
- spring的注解注入
- Spring的注入,注解
- action控制层,使用spring注解方式注入业务bean报空指向错误
- Spring的bean管理(注解方式)
- Spring使用注解的方式注入属性时的xml文件配置
- spring详解:使用注解方式注入属性
- Spring管理Struts Action
- 创建Action的两种方式Spring和Struts
- struts.objectFactory 由spring管理struts2的Action实例
- 深刻剖析spring三种注入方式以及使用注解的原理
- JAVA工程结构 && CMD使用javac提示非有效命令怎么办
- 函数间数据拷贝 stack overflow
- php 中的单 双引号
- 值得纪念的一天
- lucene:no segments* file found in org.apache.lucene.store.FSDirectory@
- 使用Spring的注解方式注入Struts所管理的action
- 机油的型号
- 愚人节就要到了,我喜欢这个网站~
- cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
- 无题
- 文理分科与文理合流
- Re-enable M-Lint
- hadoop分布式系统 3
- Graphics类和GraphicsPath类的使用