spring aop 配置及使用

来源:互联网 发布:知乐小说作品集 编辑:程序博客网 时间:2024/06/10 20:04

一、 xml配置

1.先写一个普通类:

package com.spring.aop;

public class Common {
 public void execute(String username,String password){
     System.out.println("------------------
普通类----------------");
   }

}

2.写一个切面类,用于合法性校验和日志添加:

package com.spring.aop;

public class Check {
 public void checkValidity(){
     System.out.println("------------------
验证合法性----------------");
 }

public void addLog(JoinPoint j){
  System.out.println("------------------
添加日志----------------");
  Object obj[] = j.getArgs();
   for(Object o :obj){
    System.out.println(o);
   }
   System.out.println("========checkSecurity=="+j.getSignature().getName());//
这个是获得方法名
 }

}

3.xml配置

配置Bean,打开AOP命名空间

<!-- 系统服务组件的切面Bean -->
 <bean id="common" class="com.spring.aop.Common"/>

  <bean id="check" class="com.spring.aop.Check"/>
   <aop:aspectj-autoproxy/>即自动产生代理。
  
<aop:config>
    <aop:aspect id="myAop" ref="check">
      <aop:pointcut id="target" 
expression="execution(* com.spring.aop.Common.execute(..))"/>  --- 定义需要加切面方法目标  切入点
      <aop:before method="checkValidity" pointcut-ref="target"/>
      <aop:after method="addLog" pointcut-ref="target"/>--最终通知
    </aop:aspect>
  </aop:config>

使用时:

BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext-aop.xml");
     Common c=(Common) factory.getBean("common");
     c.execute("zhengjunhua","zhengjunhua");


expression 表达式解释:

execution(* com.spring.aop.*.*(..))"/

这样写应该就可以了

这是com.spring.aop 包下所有的类的所有方法。。

第一个*代表所有的返回值类型

第二个*代表所有的类

第三个*代表类所有方法

最后一个..代表所有的参数。

二、annotation 注解形式

  1. /** 
  2.  * 切面 
  3.  * @author Bird 
  4.  * 
  5.  */  
  6. @Aspect  
  7. public class MyInterceptor {  
  8.     @Pointcut("execution(* com.bird.service.impl.PersonServiceBean.*(..))")  
  9.     private void anyMethod(){}//定义一个切入点  
  10.       
  11.     @Before("anyMethod() && args(name)")  
  12.     public void doAccessCheck(String name){  
  13.         System.out.println(name);  
  14.         System.out.println("前置通知");  
  15.     }  
  16.       
  17.     @AfterReturning("anyMethod()")  
  18.     public void doAfter(){  
  19.         System.out.println("后置通知");  
  20.     }  
  21.       
  22.     @After("anyMethod()")  
  23.     public void after(){  
  24.         System.out.println("最终通知");  
  25.     }  
  26.       
  27.     @AfterThrowing("anyMethod()")  
  28.     public void doAfterThrow(){  
  29.         System.out.println("例外通知");  
  30.     }  
  31.       
  32.     @Around("anyMethod()")  
  33.     public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{  
  34.         System.out.println("进入环绕通知");  
  35.         Object object = pjp.proceed();//执行该方法  
  36.         System.out.println("退出方法");  
  37.         return object;  
  38.     }  
  39. 或者
    //配置前置通知,拦截返回值为cn.ysh.studio.spring.mvc.bean.User的方法@Before("execution(cn.ysh.studio.spring.mvc.bean.User cn.ysh.studio.spring.aop.service..*(..))")public void beforeReturnUser(JoinPoint joinPoint){if(log.isInfoEnabled()){log.info("beforeReturnUser " + joinPoint);}}//配置前置通知,拦截参数为cn.ysh.studio.spring.mvc.bean.User的方法@Before("execution(* cn.ysh.studio.spring.aop.service..*(cn.ysh.studio.spring.mvc.bean.User))")public void beforeArgUser(JoinPoint joinPoint){if(log.isInfoEnabled()){log.info("beforeArgUser " + joinPoint);}}


0 0