Spring AOP 注解实现

来源:互联网 发布:qq显示网络状态准确吗 编辑:程序博客网 时间:2024/05/02 02:03
使用aspectj注解实现的AOP需要引入aspectj的lib
<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
原创粉丝点击