快速入门
基于注解的AOP开发步骤:
- 创建目标接口和目标类(内部有切点)
1 2 3 4 5 6 7 8 9 10 11
| package cn.imqinhao.anno;
public interface TargetInterface {
public void save();
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package cn.imqinhao.anno;
import org.springframework.stereotype.Component;
public class Target implements TargetInterface { @Override public void save() { System.out.println("save running..."); } }
|
- 创建切面类(内部有增强方法)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package cn.imqinhao.anno;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component;
public class MyAspect {
public void before() { System.out.println("前置增强..."); }
}
|
- 将目标类和切面类的对象创建权交给spring
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package cn.imqinhao.anno;
import org.springframework.stereotype.Component;
@Component("target") public class Target implements TargetInterface { @Override public void save() { System.out.println("save running..."); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package cn.imqinhao.anno;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component;
@Component("myAspect") public class MyAspect {
public void before() { System.out.println("前置增强..."); }
}
|
- 在切面类中使用注解配置织入关系
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package cn.imqinhao.anno;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component;
@Component("myAspect") @Aspect public class MyAspect {
@Before("execution(* cn.imqinhao.anno.*.*(..))") public void before() { System.out.println("前置增强..."); }
}
|
- 在配置文件中开启组件扫描和AOP的自动代理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?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" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
<context:component-scan base-package="cn.imqinhao.anno" />
<aop:aspectj-autoproxy />
</beans>
|
- 测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package cn.imqinhao.test;
import cn.imqinhao.anno.TargetInterface; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext-anno.xml")
public class AnnoTest {
@Autowired private TargetInterface target;
@Test public void test1() { target.save(); }
}
|
运行结果:
注解通知的类型
通知的配置语法:**@通知注解(“切点表达式”)**
切点表达式的抽取
同xml配置aop一样,我们可以将切点表达式抽取。抽取方式是在切面内定义方法,在该方法上使用@Pointcut注解定义切点表达式,然后在在增强注解中进行引用。具体如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package cn.imqinhao.anno;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component;
@Component("myAspect") @Aspect public class MyAspect {
@Around("pointCut()") public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("环绕前增强..."); Object proceed = proceedingJoinPoint.proceed(); System.out.println("环绕后增强..."); return proceed; }
@After("MyAspect.pointCut()") public void after() { System.out.println("最终增强..."); }
@Pointcut("execution(* cn.imqinhao.anno.*.*(..))") public void pointCut(){}
}
|
运行结果:
1 2 3 4
| 环绕前增强… save running… 环绕后增强… 最终增强…
|