spring AOP @Around @Before @After 区别
来源:互联网 发布:如何导入照片到mac 编辑:程序博客网 时间:2024/05/12 14:44
此段小代码演示了spring aop中@Around @Before @After三个注解的区别@Before是在所拦截方法执行之前执行一段逻辑。@After 是在所拦截方法执行之后执行一段逻辑。@Around是可以同时在所拦截方法的前后执行一段逻辑。
package com.itsoft.action;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.stereotype.Controller;/** * * @author zxf * 演示aop测试类 */@Controllerpublic class UserAction { public void queryUsers(){ System.out.println("查询所有用户【all users list】"); } public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("application-aop.xml"); UserAction userAction = (UserAction)ctx.getBean("userAction"); userAction.queryUsers(); ctx.destroy(); }}
package com.itsoft;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;/** * * @author Administrator * 通过aop拦截后执行具体操作 */@Aspect@Componentpublic class LogIntercept { @Pointcut("execution(public * com.itsoft.action..*.*(..))") public void recordLog(){} @Before("recordLog()") public void before() { this.printLog("已经记录下操作日志@Before 方法执行前"); } @Around("recordLog()") public void around(ProceedingJoinPoint pjp) throws Throwable{ this.printLog("已经记录下操作日志@Around 方法执行前"); pjp.proceed(); this.printLog("已经记录下操作日志@Around 方法执行后"); } @After("recordLog()") public void after() { this.printLog("已经记录下操作日志@After 方法执行后"); } private void printLog(String str){ System.out.println(str); }}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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="com.itsoft"/> <aop:aspectj-autoproxy /></beans>
原文地址:http://outofmemory.cn/code-snippet/3025/spring-AOP-Around-Before-After-differentiate
0 0
- spring AOP @Around @Before @After 区别
- spring AOP @Around @Before @After 区别
- spring AOP @Around @Before @After 区别
- spring AOP @Around @Before @After 区别
- spring AOP @Around @Before @After 区别
- spring AOP @Around @Before @After 区别
- spring AOP @Around @Before @After 区别
- spring AOP @Around @Before @After 区别
- Spring AOP @Before @Around @After 等 advice 的执行顺序
- Spring AOP @Before @Around @After 等 advice 的执行顺序
- Spring AOP @Before @Around @After 等 advice 的执行顺序
- Spring AOP @Before @Around @After 等 advice 的执行顺序
- spring aop的@Before,@Around,@After,@AfterReturn,@AfterThrowing的理解
- AOP编程@Pointcut、@AfterReturning、@before、@after、@around
- Spring AOP 中 advice 的四种类型 before after throwing advice around
- Spring AOP 中 advice 的四种类型 before after throwing advice around
- spring AOP 中 advice 的四种类型 before after throwing advice around
- Spring2.0 AOP 之Before,After,Thrown,Around Advice(一)
- centos6.5上vncserver上的相关配置
- [深度学习论文笔记][Weight Initialization] Random walk initialization for training very deep feedforward netw
- *小回顾—类(Class)和对象(Object)
- Android中手机声音调节步骤:
- 个人对安卓Studio一些简单实用的快捷键和一些插件的见解
- spring AOP @Around @Before @After 区别
- PL/SQL Developer连接oracle12出现"ORA-12154"解决方案
- Xcode8 打开的xib遇到的问题
- Android Studio 2.2 通过Layout Editor建立UI
- Android-progressdialog自定义背景透明的圆形进度条类似于Dialog
- openlayers3和openlayers4,导航栏使用放缩平移动画
- html中table居中和表格内容居中的问题
- 点趣苹果助手:非苹果独占 口袋妖怪GO将登录安卓手表
- org.apache.ibatis.ognl.MethodFailedException: Method "size" failed for object