spring-AspectJ
动态代理
ProxyFactoryBean织入切面数量太多不利于围护
BeanNameAutoProxyCreater-------------根据Bean名称创建代理
DefaultAdvisorAutoProxyCreator-------------根据Advisor本身包含信息创建代理
AnnotationAwareAspectJAutoProxyCreator-----------基于AspectJ注解进行自动代理
AspectJ AOP
1、AspectJ是一个基于Java语言的AOP框架
2、Spring2.0后支持AspectJ切点表达式支持
3、@AspectJ是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面
注解方式开发
增强类型
@Before 前置通知------------BeforeAdvice
@AfterReturning 后置通知------------AfterReturningAdvice
可以接收返回值
@Around 环绕通知------------MethodInterceptor
@AfterThrowing异常抛出通知-----------ThrowAdvice
@After 最终final通知(一定会执行)
@DeclareParents引介通知-----------IntroductionInterceptor(不要求掌握)
通知中通过value属性定义切点
Execution函数:定义切点的方法切入
语法:execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
定义切点名称
@Pointcut
切点方法:private void 无参方法(方法名为切点名)
多个切点可用||连接
开发步骤
1、 引入maven依赖包
2、 配置spring配置文件
3、 定义目标类IOC(xml格式)
4、 定义切面类@Aspect
5、 定义切面方法@增强类型(value=“execution函数”)
在函数中传入JoinPoint 参数可以获得增强信息
ProceedingJoinPoint参数可以调用拦截目标方法执行
6、 定义切面IOC
7、 测试:
@RunWith(SpringJunit4ClassRunner.class) @ContextConfiguration(“classpath:配置文件名称”)
XML方式开发
1、 配置目标类
2、 配置切面类
3、 AOP相关配置
<aop:config> <aop:pointcut id=”切入点ID” expression=”切入点函数”>-------------配置切入点 <aop:aspect ref=”切面ID”> <aop:before method=”切面方法” pointcut-ref=”切入点ID” </aop:aspect> ----------------------------------------配置切面 </aop:config>
4、 测试