主要是在类中贴注解和在配置文件中配置注解解析器
以Spring(5)-(20)中的例子为例
代码改造
(1)在IAccoutDAOImpl类中贴注解标签(⚠️为贴的注解标签)
....
@Repository//⚠️
public class IAccoutDAOImpl implements IAccoutDAO {
private JdbcTemplate jdbcTemplate;
@Autowired//⚠️:Autowired标签一定不要贴在jdbcTemplate字段上
public void setDataSource(DataSource ds) {
this.jdbcTemplate = new JdbcTemplate(ds);
}
....
(2)在IAccoutSerivceImpl类中贴注解标签(⚠️为贴的注解标签)
@Service ⚠️
@Transactional ⚠️
public class IAccoutSerivceImpl implements IAccoutService{
@Autowired ⚠️
private IAccoutDAO dao;
public void trans(int outId, int inId, int money) {
dao.transOut(outId, money);
int i = 1/0;//模仿程序出错
dao.transInt(inId, money);
}
//假如还有查询的方法(就重新注解一次,并获取它需要的属性)
@Transactional(readOnly = true)
public void ListXXX() {
}
(3)xml配置文件
....
<!-- 从classPath的路径去加载db.properties文件 -->
<context:property-placeholder location="classpath:db.properties" system-properties-mode="NEVER" />
<!-- 配置一个durid的连接池 -->
<bean id = "dataSource" class ="com.alibaba.druid.pool.DruidDataSource"
init-method = "init" destroy-method="close">
<property name = "driverClassName" value ="${dirverClassName}"/>
<property name = "url" value ="${url}"/>
<property name = "username" value ="${username}"/>
<property name = "password" value ="${password}"/>
<property name = "initialSize" value ="${initialSize}"/>
</bean>
<!-- 配置DI注解解析器 -->⚠️
<context:annotation-config/>
<!-- 配置IOC注解解析器 -->⚠️
<context:component-scan base-package="com.keen.proxy"/>
<!--1.what: 配置事务管理器的JDBC,需要一个连接池属性 -->
<bean id = "txManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref = "dataSource"/>
</bean>
<!-- 配置事务注解解析器 -->⚠️
<tx:annotation-driven transaction-manager ="txManager"/>
</beans>
其他内容不需改变,把上次配置文件放在下面,你可以对照比较并理解
这是没有使用注解标签的配置文件
...
<!-- 从classPath的路径去加载db.properties文件 -->
<context:property-placeholder location="classpath:db.properties" system-properties-mode="NEVER" />
<!-- 配置一个durid的连接池 -->
<bean id = "dataSource" class ="com.alibaba.druid.pool.DruidDataSource"
init-method = "init" destroy-method="close">
<property name = "driverClassName" value ="${dirverClassName}"/>
<property name = "url" value ="${url}"/>
<property name = "username" value ="${username}"/>
<property name = "password" value ="${password}"/>
<property name = "initialSize" value ="${initialSize}"/>
</bean>
<!-- 配置dao -->
<bean id = "accoutDAO" class = "com.keen.proxy.dao.impl.IAccoutDAOImpl">
<property name="dataSource" ref = "dataSource"/>
</bean>
<!-- 配置service -->
<bean id ="service" class = "com.keen.proxy.service.impl.IAccoutSerivceImpl">
<property name = "iAccoutDAO" ref = "accoutDAO"/>
</bean>
<!-- *********************看这里!看这里!看这里!**************************** -->
<!--1.what: 配置事务管理器的JDBC,需要一个连接池属性 -->
<bean id = "txManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref = "dataSource"/>
</bean>
<!-- 2.when:配置事务管理器增强 -->
<tx:advice id ="txAdvice" transaction-manager ="txManager">
<tx:attributes>
<tx:method name="trans"/>
</tx:attributes>
</tx:advice>
<!-- 3.where :配置切面 -->
<aop:config>
<aop:pointcut expression="execution(* com.keen.proxy.service.*Service.*(..))" id="txPc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/>
</aop:config>
<!-- *****************配置一个通用事务配置********************** -->
<tx:advice id="crudAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- service中的查询方法 -->
<tx:method name="get" read-only="true" propagation="REQUIRED"/>
<tx:method name="list" read-only="true" propagation="REQUIRED"/>
<tx:method name="query" read-only="true" propagation="REQUIRED"/>
<!-- service中的其他方法(非查询) -->
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
</beans>
网友评论