psring_Struts2_Spring3整合问题

来源:互联网 发布:mac 键盘符号 编辑:程序博客网 时间:2024/06/07 08:34

struts2里action通过class自动去spring里找bean的id为userAction

缺点:spring的bean必须设置scope范围为prototype

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!-- 指定打开.jsp页面默认编码 --><constant name="struts.i18n.encoding" value="GBK" /><!-- 与spring集合时,指定由spring负责action对象的创建(自动装配) --><constant name="struts.objectFactory.spring.autoWire" value="type" /><!-- 开发模式下使用,这样可以打印出理详细的错误信息 --><constant name="struts.dewMode" value="true" /><package name="registration" namespace="/bjsxt" extends="struts-default"><!-- 通过class自动去spring里找bean的id为userAction也可以设置为action类 --><action name="user_*" class="userAction" method="{1}"><result name="SUCCESS">/registSuccess.jsp</result><result name="FAIL">/registFail.jsp</result><!-- 使用重定向jsp页面取不到list值 --><!-- <result name="LIST" type="redirect">/userlist.jsp</result> --><result name="LIST">/userlist.jsp</result><result name="LOAD">/user.jsp</result></action></package></struts>


<?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.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 依赖注入UserDaoImpl --><bean id="userDaoImpl" class="com.bjsxt.registration.dao.impl.UserDaoImpl"><property name="hibernateTemplate" ref="hibernateTemplate" /><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 依赖注入UserManagerImpl --><bean id="userManagerImpl" class="com.bjsxt.registration.service.impl.UserManagerImpl"><property name="userDao" ref="userDaoImpl" /></bean><!-- 依赖注入UserAction,每次访问都创建一个新实例 --><bean id="userAction" class="com.bjsxt.registration.action.UserAction"scope="prototype"><property name="userManager" ref="userManagerImpl" /><property name="info" ref="info" /></bean><!-- 依赖注入UserRegisterInfo --><bean id="info" class="com.bjsxt.registration.vo.UserRegisterInfo"></bean><!-- 配置DataSource --><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><value>classpath:jdbc.properties</value></property></bean><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${jdbc.driverClassName}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><!-- 配置SessionFactory --><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="mappingDirectoryLocations"><list><value>classpath:com/bjsxt/registration/model/</value></list></property><property name="hibernateProperties"><props><!-- 显示SQL语句 --><prop key="hibernate.show_sql">true</prop><!-- 自动构建表结构 --><prop key="hibernate.hbm2ddl.auto">update</prop></props></property></bean><!-- 配置HibernateTeimplate --><bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 配置事务txManager --><bean id="txMeanage"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 配置AOP范围 --><aop:config><aop:pointcutexpression="execution(public * com.bjsxt.registration.service..*.*(..))"id="pointcut" /><aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" /></aop:config><!-- 配置事务范围txAdvisor --><tx:advice id="txAdvice" transaction-manager="txMeanage"><tx:attributes><tx:method name="exists" read-only="true" /><tx:method name="get*" read-only="true" /><tx:method name="add*" propagation="REQUIRED" /></tx:attributes></tx:advice></beans>


原创粉丝点击