java AOP使用注解@annotation方式实践

AOP 实际开发工作比较常用,在此使用注解方式加深对面向切面编程的理解

废话不多少,先看下面的实例代码

场景:

1. 未满一岁的小孩,在执行方法之前打印:“还没有满一岁,不会说话”,在执行方法之后打印:“请多多关注我!”

2. 大于一岁的小孩,在执行方法之前打印:“大家好, 我的名字叫: "+ baby.getName()+" 我今年 "+ baby.getAge() +" 岁了!”,在执行方法之后打印:“请多多关注我!”

切面类:定义切点和切面方法

@Aspect
@Component
public class CheckAgeAspect {
@Autowired
Baby baby;

@Pointcut(</span>"@annotation(com.mb.util.CheckAge)")  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">@annotation声明以注解的方式来定义切点</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> checkDataPoint(){

}
@Before(</span>"checkDataPoint()")  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">前置通知</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> checkBefore(){
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (baby.getAge() &lt;= 1<span style="color: rgba(0, 0, 0, 1)">) {
        System.out.println(</span>"还没有满一岁,不会说话"<span style="color: rgba(0, 0, 0, 1)">);
    }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
        System.out.println(</span>"大家好,我的名字叫: "+  baby.getName() + "我今年 "+ baby.getAge() +" 岁了!"<span style="color: rgba(0, 0, 0, 1)">);
    }
}
@After(</span>"checkDataPoint()") <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">后置通知</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> checkAfter(){
    System.out.println(</span>"请多多关注我!"<span style="color: rgba(0, 0, 0, 1)">);
}

}

/**
after returning 目标方法返回时执行 ,后置返回通知
after throwing 目标方法抛出异常时执行 异常通知
around 在目标函数执行中执行,可控制目标函数是否执行,环绕通知
以上三种通知使用上基本雷同不举例了
/

注解方法:使用注解的方式来定义,在目标方法上面标注该注解

@Target(ElementType.METHOD)   //定义注解的使用范围为方法
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckAge {

}

目标类

@Data  //需要导入 import lombok.Data; 通过注解搞定 getter 和 setter
@Component
public class Baby {
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age;
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String name;

@CheckAge
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> say(){
    System.out.println(</span>"执行目标方法"<span style="color: rgba(0, 0, 0, 1)">);
}

}

applicationContext.xml 

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">context:component-scan </span><span style="color: rgba(255, 0, 0, 1)">base-package</span><span style="color: rgba(0, 0, 255, 1)">="com.mb"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">context:component-scan</span><span style="color: rgba(0, 0, 255, 1)">&gt;   &lt;!--项目包扫描--&gt;</span></pre>
<aop:aspectj-autoproxy proxy-target-class="false"></aop:aspectj-autoproxy> <!-- 使用注解的方式,需要添加该标签,它会调用 AspectJAutoProxyBeanDefinitionParser 的 parse 来解析;   proxy-target-class为 true 则是基于类的代理将起作用(需要 cglib 库),为 false 或者省略这个属性,则标准的 JDK 基于接口的代理将起作用,一般采用后者,因为前者效率高 -->
</beans>

测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:**/applicationContext*.xml"})
public class DemoTest {
@Autowired
Baby person;
@Test
public void demoTest(){
person.setAge(3);
person.setName("美美");
person.say();
}
}
执行结果:

大家好, 我的名字叫: 美美我今年 3 岁了!
执行目标方法
请多多关注我!