Spring AOP
来源:互联网 发布:电影php用什么打开 编辑:程序博客网 时间:2024/05/22 00:47
spring - aop 使用方式总结
方式一:接口
前置增强 MethodBeforeAdvice
后置增强 AfterReturningAdvice
异常抛出增强 ThrowsAdvice
环绕增强 MethodInterceptor
注意:还需要在applicationContext.xml文件中进行aop相关的配置
<aop:config>
<aop:pointcut expression="execution(public void *User())" id="addoptpointcut"/>
<aop:advisor advice-ref="logbefore" pointcut-ref="addoptpointcut" />
</aop:config>
<aop:config>
<aop:pointcut expression="execution(* spring_basic.UserService.*(..))" id="userServiceOptPointcut"/>
<!--
<aop:advisor advice-ref="exceptionAdvice" pointcut-ref="userServiceOptPointcut" />
-->
<aop:advisor advice-ref="logger" pointcut-ref="userServiceOptPointcut" />
</aop:config>
方式二:注解
前置增强 @Before("execution(* service.*.*(..))")
后置增强 @AfterReturning(pointcut="execution(* service.*.*(..))", returning="result")
异常抛出增强 @AfterThrowing(pointcut="execution(* service.*.*(..))", throwing="ex")
环绕增强 @Around("execution(* service.*.*(..))")
注意:还需要在applicationContext.xml文件中添加如下配置
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
方式三:Scheme
首先:添加普通的类,里面按要求编写方法
public class Logger {
public void before(JoinPoint jp){
}
public void after(JoinPoint jp, Object result){
}
public void aterThrowing(JoinPoint jp, Exception ex){
}
public Object aroundExecute(ProceedingJoinPoint pjp) throws Throwable{
}
}
其次:在applicationContext.xml中配置aop-aspect相关的配置
<aop:config>
<aop:pointcut expression="execution(* service.*.*(..))" id="optpointcut"/>
<aop:aspect ref="mylogger">
<aop:before method="before" pointcut-ref="optpointcut" />
<aop:after-returning method="after" returning="result" pointcut-ref="optpointcut" />
<aop:after-throwing method="aterThrowing" throwing="ex" pointcut-ref="optpointcut" />
<aop:around method="aroundExecute" pointcut-ref="optpointcut"/>
</aop:aspect>
</aop:config>
方式一:接口
前置增强 MethodBeforeAdvice
后置增强 AfterReturningAdvice
异常抛出增强 ThrowsAdvice
环绕增强 MethodInterceptor
注意:还需要在applicationContext.xml文件中进行aop相关的配置
<aop:config>
<aop:pointcut expression="execution(public void *User())" id="addoptpointcut"/>
<aop:advisor advice-ref="logbefore" pointcut-ref="addoptpointcut" />
</aop:config>
<aop:config>
<aop:pointcut expression="execution(* spring_basic.UserService.*(..))" id="userServiceOptPointcut"/>
<!--
<aop:advisor advice-ref="exceptionAdvice" pointcut-ref="userServiceOptPointcut" />
-->
<aop:advisor advice-ref="logger" pointcut-ref="userServiceOptPointcut" />
</aop:config>
方式二:注解
前置增强 @Before("execution(* service.*.*(..))")
后置增强 @AfterReturning(pointcut="execution(* service.*.*(..))", returning="result")
异常抛出增强 @AfterThrowing(pointcut="execution(* service.*.*(..))", throwing="ex")
环绕增强 @Around("execution(* service.*.*(..))")
注意:还需要在applicationContext.xml文件中添加如下配置
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
方式三:Scheme
首先:添加普通的类,里面按要求编写方法
public class Logger {
public void before(JoinPoint jp){
}
public void after(JoinPoint jp, Object result){
}
public void aterThrowing(JoinPoint jp, Exception ex){
}
public Object aroundExecute(ProceedingJoinPoint pjp) throws Throwable{
}
}
其次:在applicationContext.xml中配置aop-aspect相关的配置
<aop:config>
<aop:pointcut expression="execution(* service.*.*(..))" id="optpointcut"/>
<aop:aspect ref="mylogger">
<aop:before method="before" pointcut-ref="optpointcut" />
<aop:after-returning method="after" returning="result" pointcut-ref="optpointcut" />
<aop:after-throwing method="aterThrowing" throwing="ex" pointcut-ref="optpointcut" />
<aop:around method="aroundExecute" pointcut-ref="optpointcut"/>
</aop:aspect>
</aop:config>
阅读全文
0 0
- AOP、Spring的AOP
- AOP--Spring AOP
- Spring AOP 嵌套AOP
- spring AOP
- Spring AOP
- Spring AOP
- spring aop
- Spring AOP
- spring AOP
- spring aop
- Spring aop
- Spring-AOP
- Spring AOP
- spring aop
- spring aop
- Spring AOP
- Spring AOP
- Spring Aop
- JDBC调用存储过程
- Unity 3D学习日记(3)
- POJ2632 Crashing Robots(模拟)
- 一步一步写MP3项目的Makefile--自动添加目标对头文件的依赖
- js中的prototype的用法
- Spring AOP
- STM32用串口(USB串口)下载程序的方法
- java 子类继承父类运行顺序
- 网络经济与企业管理
- Scala Option(选项)
- Eclipse Project facet Java version 1.8 is not supported问题解决方法
- ImageView(图像视图)-src属性和background属性的区别:
- 2.Maven基础配置—上传jar包到私服
- Script 入门实践