spring的AOP配置

spring的AOP配置

切入点表达式

关键字:execution(表达式)
表达式写法:```

  • cn.imust.service.impl..(…)
# 基于XML的配置步骤

 1. 把通知bean也交给spring来管理
 2. 使用aop:config标签表明开始AOP配置
 3. 使用aop:aspect标签表明配置切面
 						- id属性:给切面提供一个唯一标识
 						- ref属性:是指定通知类的id(**即增强方法的类**)
 4. 在aop:aspect标签的内部使用对应的标签来配置统治的类型
 						- **aop:before**:表示配置前置通知
 	method属性:用于指定增强类中的哪个方法是前置通知
 	pointcut属性:指定切入点表达式(指对应业务层中的哪个方法)
 						- **加粗样式**
<aop:config>
    <!--配置切面-->
    <aop:pointcut id="pt1" expression="execution(* cn.imust.service.impl.AccountServiceImpl.saveAccount())"></aop:pointcut>

    <aop:aspect id="logAdvice" ref="logger">
        <!--配置通知的类型,并且建立通知方法和切入点的关联-->
        <!--<aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before>-->
        <!--<aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>-->
        <!--<aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>-->
        <!--<aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>-->
        //环绕类型
        <aop:around method="aroundPrintLog" pointcut-ref="pt1"></aop:around>
    </aop:aspect>
</aop:config>
在增强类中,编写环绕方法

//环绕通知
//问题:配置好以后,切入点方法没有执行,而通知方法执行了
//分析:发现动态代理中环绕通知有明确的切入点方法调用,而这个没有
//解决:spring提供了一个接口:ProceedingJoinPoint
// 这个接口提供了一个方法,proceed();
//环绕通知:它是spring框架为我们提供的一种可以在代码中手动控制增强方法何时执行的方式
public Object aroundPrintLog (ProceedingJoinPoint pjp){
Object rtValue=null;
try {
Object[] args = pjp.getArgs();
System.out.println(“aroundPrintLog方法开始记录日志。。。前置”);
rtValue = pjp.proceed(args);//调用业务层方法
System.out.println(“aroundPrintLog方法开始记录日志。。。后置”);

        return rtValue;

    } catch (Throwable t) {
        System.out.println("aroundPrintLog方法开始记录日志。。。异常");
        throw new RuntimeException(t);
    }finally {
        System.out.println("aroundPrintLog方法开始记录日志。。。最终");

    }
}


# 基于注解的AOP步骤
1. @Aspect表示表示当前类为增强类及切面类
2. @Before()前置
3. @AfterReturning后置通知
4. @AfterThrowing异常通知
5. @After最终通知
6. @Around()环绕通知
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP配置可以通过XML方式或注解方式进行。 XML方式的配置步骤如下: 1. 在Spring配置文件中引入aop的约束,例如:xmlns:aop="http://www.springframework.org/schema/aop"。 2. 把通知Bean交给Spring管理,使用<bean>标签进行配置。 3. 使用<aop:config>标签开始AOP配置。 4. 使用<aop:aspect>标签配置切面。 5. 使用对应的标签配置通知的类型,例如<aop:before>表示前置通知。 6. 在<aop:before>标签中指定通知方法和切入点表达式。 注解方式的配置步骤如下: 1. 在配置类上使用@Configuration注解进行标记。 2. 使用@ComponentScan注解指定需要扫描的包。 3. 使用@EnableAspectJAutoProxy注解开启Spring对注解AOP的支持。 以上是关于Spring AOP配置的简要说明,具体的配置内容可以参考引用的资料\[1\]、\[2\]和\[3\]。 #### 引用[.reference_title] - *1* *3* [Spring AOP 应用:三种配置及实现方式](https://blog.csdn.net/qq_37829947/article/details/117955529)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【spring配置】——spring aop配置](https://blog.csdn.net/javawebxy/article/details/50492616)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值