Spring AOP 注解实现
来源:互联网 发布:qq显示网络状态准确吗 编辑:程序博客网 时间:2024/05/02 02:03
使用aspectj注解实现的AOP需要引入aspectj的lib
1. 首先启用 Spring 对 @AspectJ 切面配置的支持
2.定义切面 Bean
3. 目标Bean
4 测试
测试结果:
enter method: Peter
doBefore: Peter
doBefore2: Peter
Peter want to login.
exit method
doAfter
doAfterReturning:null
<dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.8.0</version></dependency> <dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.0</version></dependency>
1. 首先启用 Spring 对 @AspectJ 切面配置的支持
<?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.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsd"><!-- scan all beans and inject dependence --><context:component-scan base-package="com.myproject"/><aop:aspectj-autoproxy/></beans>
2.定义切面 Bean
@Component@Aspect public class LogInterceptor {@Pointcut("execution (!void com.myproject.example.service..*(..))") private void anyUnvoid(){} @Pointcut("within(com.myproject.example.service..*)") private void anyService(){} @Pointcut("anyUnvoid() && anyService()") private void anyUnvoidService(){} @Before("anyUnvoidService() && args(username, password)")public void doBefore(String username, String password){System.out.println("doBefore: "+username);} /** * 第二种访问目标参数的方法。<br> * 将第一个参数定义为 JoinPoint 类型,当该增强处理方法被调用时, * 该 JoinPoint 参数就代表了织入增强处理的连接点。 */@Before("anyUnvoidService()")public void doBefore2(JoinPoint jp){Object[] args = jp.getArgs();System.out.println("doBefore2: " +args[0]);} @AfterReturning(pointcut="anyUnvoidService()", returning="result")public void doAfterRunning(String result){System.out.println("doAfterReturning:" +result);}@AfterReturning(pointcut="execution(* com.myproject.example.service.AccountService.Login(..))", returning="result")public void doAfterLogin(JoinPoint jp, Account result){if(result!=null)System.out.println(result.getUsername() +" login success.");elseSystem.out.println(jp.getArgs()[0] +" login fail.");}@AfterThrowing(pointcut="anyUnvoidService()", throwing="ex")public void doAfterThrowing(Throwable ex){System.out.print("doAfterThrowing: "+ex.getMessage());} @After("anyUnvoidService()") public void doAfter(){ System.out.println("doAfter");} /** * Around 增强处理可替代所有其它增强处理。 * 它可改变执行目标方法的参数值,也可改变目标方法之后的返回值。 */@Around("anyUnvoidService()")public Object doAround(ProceedingJoinPoint pjp) throws Throwable{Object[] args = pjp.getArgs(); //obtain arguementsSystem.out.println("enter method: "+args[0]);Object result = pjp.proceed();System.out.println("exit method");return result;}}
3. 目标Bean
@Servicepublic class AccountService {@Resourceprivate AccountDao accountDao;public Account Login(String username, String password){System.out.println(username + " want to login.");return null;}}
4 测试
@Testpublic void test(){ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");AccountService accountService =(AccountService)ac.getBean("accountService");accountService.Login("Peter", "wetwe");}
测试结果:
enter method: Peter
doBefore: Peter
doBefore2: Peter
Peter want to login.
exit method
doAfter
doAfterReturning:null
阅读全文
0 0
- 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
- spring 注解方式实现aop
- Spring 注解方式实现AOP
- Head First设计模式笔记(模板方法模式)
- mui开发APP教程之mui.ajax请求后出现“加载中”
- Java 动态代理2
- CentOS 7 使用阿里云的yum源
- jquery ui datetimepicker 弹出方向
- Spring AOP 注解实现
- csdn如何转载
- 金融风控-->客户流失预警模型-->神经网络建模
- Spring AOP XML配置实现
- Spring + jdbc +connection pool + transaction
- Python+Selenium--断言
- 水题 第四站 HDU A + B Problem II
- 索引类型
- HM模型学习(一)