知识点回顾
@Controller @Component @Service @Repository @Autowired @Qualifier @Resource @Value @Bean @Scope @PropertySource @Import @ContextConfiguration
转账案例
A->B->100 1.查询A账号信息 2.查询B账号信息 3.A-money 4.B+money 5.将数据同步到数据库
SpringXMLAOP配置
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- LogHandler --> <bean id="logHandler" class="com.itheima.log.LogHandler"></bean> <!-- AccountServiceImpl --> <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean> <!-- aop:config 告诉Spring此处属于aop配置 aop:aspect 切面配置 指定切点->告诉Spring对哪个方法进行增强 指定通知类型->前置通知、后置通知、异常通知、最终通知、环绕通知 --> <aop:config> <!-- aop:aspect 切面配置 --> <aop:aspect ref="logHandler"> <!-- 指定切点 java.lang.Integer com.itheima.service.impl.AccountServiceImpl.add(java.lang.String) 指定通知类型 --> <!-- 前置通知 pointcut="":指定切点,对该方法进行增强 method="":要执行增强的方法 --> <aop:before method="beforeLog" pointcut="execution(java.lang.Integer com.itheima.service.impl.AccountServiceImpl.add(java.lang.String))" /> <!-- 后置通知 --> <aop:after-returning method="afterLog" pointcut="execution(void com.itheima.service.impl.AccountServiceImpl.delete())" /> <!-- 异常通知 --> <aop:after-throwing method="throwExceptionLog" pointcut="execution(java.lang.String com.itheima.service.impl.AccountServiceImpl.query())" /> <!-- 最终通知 --> <aop:after method="afterLog" pointcut="execution(java.lang.Integer com.itheima.service.impl.AccountServiceImpl.add(java.lang.String)) || execution(void com.itheima.service.impl.AccountServiceImpl.delete()) || execution(java.lang.String com.itheima.service.impl.AccountServiceImpl.query())" /> <!-- 环绕通知:模拟了动态代理的整个过程 --> <aop:around method="aroundLog" pointcut="execution(void com.itheima.service.impl.AccountServiceImpl.delete())" /> </aop:aspect> </aop:config> </beans>
SpringAOP-Annotation-配置类
@Configuration @ComponentScan(basePackages = "com.itheima") @EnableAspectJAutoProxy public class SpringConfig { }
SpringAOP-Annotation-切面类配置
@Component @Aspect public class LogHandler { @Pointcut(value = "execution(* com.itheima.service.impl.*.*(..))") public void pointcut(){} /**** * 记录日志beforeLog */ @Before(value = "pointcut()") public void beforeLog(){ System.out.println("记录日志-----beforeLog"); } /**** * 记录日志afterReturningLog */ @AfterReturning(value = "pointcut()") public void afterReturningLog(){ System.out.println("记录日志-----AfterReturning"); } /*** * 最终通知 */ @After(value = "pointcut()") public void afterLog(){ System.out.println("记录日志-----afterLog"); } /**** * 记录日志throwExceptionLog */ @AfterThrowing(value = "execution(* com.itheima.service.impl.*.*(..))") public void throwExceptionLog(){ System.out.println("记录日志-----throwExceptionLog"); } /**** * 环绕通知调用方法 * ProceedingJoinPoint:当前被调用的方法签名 */ @Around(value = "execution(* com.itheima.service.impl.*.*(..))") public Object aroundLog(ProceedingJoinPoint pj){ Object result = null; try { System.out.println("前置增强了。。。"); //目标方法调用 result = pj.proceed(); System.out.println("后置增强了。。。"); } catch (Throwable throwable) { throwable.printStackTrace(); System.out.println("异常增强了。。。"); }finally { System.out.println("最终增强了。。。"); } return result; } }