AOP实际开发工作比较常用,在此使用注解方式加深对面向切面编程的理解
废话不多少,先看下面的实例代码
场景:
1.未满一岁的小孩,在执行方法之前打印:“还没有满一岁,不会说话”,在执行方法之后打印:“请多多关注我!”
2.大于一岁的小孩,在执行方法之前打印:“大家好,我的名字叫: "+ baby.getName() + "我今年 “+ baby.getAge() +” 岁了!”,在执行方法之后打印:“请多多关注我!”
切面类:定义切点和切面方法
@Aspect
@Component
public class CheckAgeAspect {
@Autowired
Baby baby;
@Pointcut("@annotation(com.mb.util.CheckAge)") //@annotation声明以注解的方式来定义切点
public void checkDataPoint(){
}
@Before("checkDataPoint()") //前置通知
public void checkBefore(){
if (baby.getAge() <= 1) {
System.out.println("还没有满一岁,不会说话");
}else {
System.out.println("大家好,我的名字叫: "+ baby.getName() + "我今年 "+ baby.getAge() +" 岁了!");
}
}
@After("checkDataPoint()") //后置通知
public void checkAfter(){
System.out.println("请多多关注我!");
}
}
/**
after returning 目标方法返回时执行 ,后置返回通知
after throwing 目标方法抛出异常时执行 异常通知
around 在目标函数执行中执行,可控制目标函数是否执行,环绕通知
以上三种通知使用上基本雷同不举例了
**/
注解方法:使用注解的方式来定义,在目标方法上面标注该注解
@Target(ElementType.METHOD) //定义注解的使用范围为方法
@Retention(RetentionPolicy.RUNTIME )
public @interface CheckAge {
}
目标类
@Data //需要导入import lombok.Data; 通过注解搞定getter和setter
@Component
public class Baby {
private int age;
private String name;
@CheckAge
public void say(){
System.out.println("执行目标方法");
}
}
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">
<context:component-scan base-package="com.mb"></context:component-scan> <!--项目包扫描-->
<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 岁了!
执行目标方法
请多多关注我!