0


基于注解的AOP开发

基于注解的AOP开发

快速入门,基于注解的aop开发步骤

①创建目标接口和目标类(内部有切点)

②创建切面类(内部有增强方法)

③将目标类和切面类的对象创建权交给spring

④在切面类中使用注解配置织入关系

⑤在配置文件中开启组件扫描和AOP的自动代理

⑥测试

编写测试

其中Target类下

  1. package anno;
  2. import org.springframework.stereotype.Component;
  3. //交给spring容器,起个名为target
  4. @Component("target")
  5. public class Target implements TargetInterface {
  6. public void save() {
  7. System.out.println("save running。。。");
  8. }
  9. }

Interface类下

  1. package anno;
  2. public interface TargetInterface {
  3. public void save();
  4. }

MyAspect切面类下

  1. package anno;
  2. import org.aspectj.lang.annotation.Aspect;
  3. import org.aspectj.lang.annotation.Before;
  4. import org.springframework.stereotype.Component;
  5. //交给spring容器
  6. @Component("myAspect")
  7. @Aspect //标注当前MyAspect是一个切面类
  8. public class MyAspect {
  9. @Before("execution(* anno.*.*(..))")
  10. public void before(){
  11. System.out.println("前置增强....");
  12. }
  13. }

AnnoTest测试类下

  1. package anno;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.test.context.ContextConfiguration;
  6. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  7. @RunWith(SpringJUnit4ClassRunner.class)
  8. @ContextConfiguration("classpath:applicationContext_anno.xml")
  9. public class AnnoTest {
  10. @Autowired
  11. private TargetInterface target;
  12. @Test
  13. public void test1(){
  14. target.save();
  15. }
  16. }

applicationContext_anno.xml配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  9. ">
  10. <!--组件扫描-->
  11. <context:component-scan base-package="anno"/>
  12. <!--aop自动代理,加上才会识别这些通知的注解-->
  13. <aop:aspectj-autoproxy/>
  14. </beans>

运行结果

注解配置AOP详解

注解通知的类型

通知的配置语法:@通知注解("切点表达式")

切点表达式的抽取

同xml配置aop一样。我们可以将切点表达式抽取,抽取方式是在切面内定义方法,早该方法上使用**@Pointcut注解**定义切点表达式,然后在在增强注解中进行引用。

切面类中

  1. package anno;
  2. import org.aspectj.lang.annotation.AfterReturning;
  3. import org.aspectj.lang.annotation.Aspect;
  4. import org.aspectj.lang.annotation.Before;
  5. import org.aspectj.lang.annotation.Pointcut;
  6. import org.springframework.stereotype.Component;
  7. //交给spring容器
  8. @Component("myAspect")
  9. @Aspect //标注当前MyAspect是一个切面类
  10. public class MyAspect {
  11. @Before("execution(* anno.*.*(..))")
  12. public void before(){
  13. System.out.println("前置增强....");
  14. }
  15. //引入切点表达式方法
  16. @AfterReturning("pointcut()")//或者写MyAspect.pointcut()
  17. public void afterReturn(){
  18. System.out.println("后置增强....");
  19. }
  20. //定义切点表达式方法
  21. @Pointcut("execution(* anno.*.*(..))")
  22. public void pointcut(){ }
  23. }

运行结果

标签: junit spring java

本文转载自: https://blog.csdn.net/weixin_60719453/article/details/126318961
版权归原作者 执久呀 所有, 如有侵权,请联系我们删除。

“基于注解的AOP开发”的评论:

还没有评论