首先配置 datasource
我的数据源配置 来自于tomcat下 conf/context.xml
在这里你可以配置数据源信息 :
context.xml 配置
<Resource name="jdbc/testDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.1.246:3306/TestDB?useUnicode=true&characterEncoding=utf-8"/>
dbonfig.properties文件 配置
jndiName=testDB
下面是applicationContextAction.xml配置文件
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value> WEB-INF/classes/config/dbonfig.properties </value> </list> </property> </bean> <!--jndiName来自 配置文件--> <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:/comp/env/jdbc/${jndiName}</value> </property> </bean>
事务配置
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 将所有具有@Transactional注解的Bean自动配置为声明式事务支持 --> <tx:annotation-driven transaction-manager="transactionManager" />
具体的DAO 及Service的配置 :设置注入
<bean id="sampleDAO" class="com.test.dao.SampleDAOImplBG" autowire="byName" /> <bean id="sampleDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="proxyInterfaces"> <list> <value>com.test.dao.SampleDAO</value> </list> </property> <property name="target" ref="sampleDAO" /> <property name="transactionManager" ref="transactionManager" /> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED,-Exception</prop> </props> </property> </bean> <bean id="sampleService" class="com.test.business.SampleServiceImpl"> <property name="sampleDAO" ref="sampleDAOProxy" /> </bean>
struts2 Action的配置:构造器注入
<bean id="SampletAction" class="com.test.web.SampleAction" scope="prototype"> <constructor-arg ref="sampleService" /> </bean>
其他配置 可以参考下面超链
http://topinking.iteye.com/blog/248839
其实spring的 声明式事务管理配置,有5种方式 :下面是更为清晰的
根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下
http://jiake.iteye.com/blog/599418