<?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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
...
</bean> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* cn.adan.bo..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="select*" read-only="true" propagation="NOT_SUPPORTED"/>
<tx:method name="insert*" read-only="true" propagation="REQUIRED"/>
<tx:method name="update*" read-only="true" propagation="REQUIRED"/>
<tx:method name="delete*" read-only="true" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> <bean id="userBO" class="cn.adan.bo.UserBO">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="personBO" class="cn.adan.bo.PersonBO">
<property name="dataSource" ref="dataSource"/>
</bean>
...
</beans>
本人新手,刚接触Spring。上面是我的Spring配置文件,我想对cn.adan.bo.*下面的所有类的方法进行事务管理。然后现在不明白的地方就是 必须每一个BO类都需要配置一个dataSource么?比如这一段:
<bean id="userBO" class="cn.adan.bo.UserBO">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="personBO" class="cn.adan.bo.PersonBO">
<property name="dataSource" ref="dataSource"/>
</bean>
...
因为我发现如果不配置的话,这个BO类里面我通过
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
这样就获取不到数据库连接,请大家帮助,有没有好的方法就是只需要对这些所有的bo类配置一个dataSource,然后也能实现这个事务管理。谢谢了!
注解的方法我具体还不是很清楚。是不是在XML里面加段这样的代码就可以了?
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
<context:component-scan base-package="cn.adan.bo"/>这样我试过就是获取不到数据库连接。在BO里面具体是用些什么样的注解我就不知道了,请教!或者给我个简单的例子也行
{
private JdbcTemplate jdbcTemplate; public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public boolean insert(User user)
{
...
}
比如这样的一个BO,我该加些怎样的注解?