spring中aop的使用

知识点回顾

@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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值