目录
XML中配置AOP
XML中配置事务管理
XML中配置AOP
切面类 正常写通知 不用加注解
代码语言:javascript复制public class MyAspect {
//前置通知
public void mybefore(){
System.out.println("前置通知");
}
//后置通知
public void myaftereturning(Object obj){
System.out.println("后置通知");
}
//环绕通知
public void myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕通知11");
Object proceed = proceedingJoinPoint.proceed();
System.out.println("环绕通知11");
}
//最终通知
public void myafter(){
System.out.println("最终通知");
}
//异常通知
public void myafterThrowing(Throwable e){
System.out.println("异常通知");
System.out.println(e.getMessage());
}
}
目标类
代码语言:javascript复制public class UserServiceImpl implements UserService {
public void eat(){
int i = 1 / 0;
System.out.println("吃饭");
}
}
xml:
步骤:
1.配置UserServiceImpl 将UserServcieImpl放入Spring容器
2.配置切面类
3.配置切面 切入点 通知
代码语言:javascript复制<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--配置userService 将UserServiceImpl放入SpringIoc容器-->
<bean id = "userService" class = "com.czxy.demo04.service.impl.UserServiceImpl"></bean>
<!--配置切面类-->
<bean id = "myAspect" class="com.czxy.demo04.aspect.MyAspect"></bean>
<!--配置切面-->
<aop:config>
<aop:aspect ref = "myAspect">
<!--切入点-->
<aop:pointcut id = "myPointcut" expression = "execution(* com.czxy.demo04.service.impl.UserServiceImpl.*(..))"></aop:pointcut>
<aop:before method = "mybefore" pointcut-ref = "myPointcut"></aop:before>
<aop:after-returning method="myaftereturning" pointcut-ref="myPointcut" returning="obj"></aop:after-returning>
<aop:around method="myAround" pointcut-ref="myPointcut"></aop:around>
<aop:after method="myafter" pointcut-ref="myPointcut"></aop:after>
<aop:after-throwing method="myafterThrowing" pointcut-ref="myPointcut" throwing="e"></aop:after-throwing>
</aop:aspect>
</aop:config>
</beans>
测试类
代码语言:javascript复制@RunWith(SpringRunner.class)
@ContextConfiguration(locations = {"classpath:demo04.xml"})
public class TestA {
@Resource(name = "userService")
private UserService userService;
@Test
public void test01(){
userService.eat();
}
}
XML中配置事务管理
转账 当出现错误的时候 转账失败 并且余额不变
xml配置类步骤:
1.加载属性配置文件
2.配置连接池
3.配置sessionFactory
4.扫描dao的包
5.配置service
6.定义事务管理器
7.配置事务属性
8.配置事务切入点
domain:
代码语言:javascript复制@Entity(name = "account")
public class Account {
@Id
private Integer id;
private String name;
private Float money;
mapper:
代码语言:javascript复制public interface AccountMapper extends Mapper<Account> {
}
转账类:
代码语言:javascript复制public class AccountServiceImpl implements AccountService {
private AccountMapper accountMapper;
public void setAccountMapper(AccountMapper accountMapper) {
this.accountMapper = accountMapper;
}
public void change(Integer outId, Integer inId, Float money) {
Account outAccount = accountMapper.selectByPrimaryKey(outId);
outAccount.setMoney(outAccount.getMoney() - money);
accountMapper.updateByPrimaryKey(outAccount);
int i = 1 / 0;
Account inAccount = accountMapper.selectByPrimaryKey(inId);
inAccount.setMoney(inAccount.getMoney() money);
accountMapper.updateByPrimaryKey(inAccount);
}
}
测试类:
代码语言:javascript复制@RunWith(SpringRunner.class)
@ContextConfiguration(locations = "classpath:demo05.xml")
public class TestA {
@Resource(name = "accountService")
private AccountService accountService;
@Test
public void testDemo(){
accountService.change(1,2,3f);
System.out.println("转账成功");
}
}
xml配置类:
代码语言:javascript复制<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 1.1 指定配置文件的位置-->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 1.2 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 配置初始化大小、最小、最大连连接数量 -->
<property name="initialSize" value="5"/>
<property name="minIdle" value="10"/>
<property name="maxActive" value="20"/>
</bean>
<!-- 2.1 sessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.czxy.demo05.domain" />
<property name="configuration">
<bean class="org.apache.ibatis.session.Configuration">
<property name="mapUnderscoreToCamelCase" value="true" />
</bean>
</property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<props>
<prop key="dialect">mysql</prop>
<prop key="rowBoundsWithCount">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
<!-- 2.2 扫描Dao的包 -->
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定会话工厂 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 指定 mybatis 接口所在的包 -->
<property name="basePackage" value="com.czxy.demo05.mapper"/>
</bean>
<!-- 3 配置service -->
<bean id="accountService" class="com.czxy.demo05.service.impl.AccountServiceImpl">
<property name="accountMapper" ref="accountMapper" />
</bean>
<!-- 4 配置事务 -->
<!-- 4.1 定义事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 4.2 配置事务属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name="change" propagation="REQUIRED"/>
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 4.3 配置事务切入点 -->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.czxy.demo05.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
</aop:config>
</beans>