spring aop的使用(注解方式以及基于xml配置方式)

转载的。。原文地址

http://blog.csdn.net/xzf19901108/article/details/7835558

注解方式

 

*******************

beans.xml

*******************

 

[html]  view plain copy
 
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xsi:schemaLocation="  
  7.            http://www.springframework.org/schema/beans  
  8.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.            http://www.springframework.org/schema/context   
  10.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  11.            http://www.springframework.org/schema/aop  
  12.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  13.            ">  
  14.              
  15.           <aop:aspectj-autoproxy/>  
  16.           <bean id="myInterceptor" class="blog.service.MyInterceptor"/>  
  17.           <bean id="personService" class="blog.service.impl.PersonServiceBean"></bean>  
  18.             
  19. </beans>  



 

 

***************

MyInterceptor.java

***************

 

[java]  view plain copy
 
 
  1. package blog.service;  
  2.   
  3. import org.aspectj.lang.ProceedingJoinPoint;  
  4. import org.aspectj.lang.annotation.After;  
  5. import org.aspectj.lang.annotation.AfterReturning;  
  6. import org.aspectj.lang.annotation.AfterThrowing;  
  7. import org.aspectj.lang.annotation.Around;  
  8. import org.aspectj.lang.annotation.Aspect;  
  9. import org.aspectj.lang.annotation.Before;  
  10. import org.aspectj.lang.annotation.Pointcut;  
  11.   
  12. /** 
  13.  * 切面 
  14.  * 
  15.  */  
  16. @Aspect  
  17. public class MyInterceptor {  
  18.     @Pointcut("execution (* blog.service.impl.PersonServiceBean.*(..))")  
  19.     public void anyMethod() {  
  20.         //表达式解释:* blog.service.impl.PersonServiceBean.*(..):第一个*号表示返回类型;  
  21.         //blog.service.impl.PersonServiceBean.*:表示PersonServiceBean类下的所有方法  
  22.         //blog.service.impl..*.*:表示blog.service.impl包及其子包下的所有类的所有方法  
  23.         //(..):表示所有的参数类型几个数都不限  
  24.     }// 声明一个切入点  
  25.   
  26.     //会拦截参数签名位 (String , int)或(String , Integer )类型的方法,  
  27.     //参数顺序与args(uname,id)一致,参数类型由(int id,String uname)决定  
  28.     @Before("anyMethod() && args(uname,id)")  
  29.     public void doAccessCheck(int id,String uname) {  
  30.         System.out.println("前置通知:" + uname + id);  
  31.     }  
  32.   
  33.     @AfterReturning(pointcut="anyMethod()",returning="result")  
  34.     public void doAfterReturning(String result) {  
  35.         System.out.println("后置通知:" + result);  
  36.     }  
  37.   
  38.     @After("anyMethod()")  
  39.     public void doAfter() {  
  40.         System.out.println("最终通知");  
  41.     }  
  42.   
  43.     @AfterThrowing(pointcut="anyMethod()",throwing="e")  
  44.     public void doAfterThrowing(Exception e) {  
  45.         System.out.println("例外通知:" + e.getMessage());  
  46.     }  
  47.       
  48.     @Around("anyMethod()")  
  49.     public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{  
  50.         //if(){//判断用户是否有权限  
  51.         System.out.println("进入方法");  
  52.         Object result = pjp.proceed();  
  53.         System.out.println("退出方法");  
  54.         //}  
  55.         return result;  
  56.     }  
  57. }  



 

***************

PersonService.java

***************

 

[java]  view plain copy
 
 
  1. package blog.service;  
  2.   
  3.   
  4. public interface PersonService {  
  5.     public String save(String name);  
  6.     public String update(String name,Integer userId);  
  7.     public String delete(int id,String dept);  
  8.       
  9. }  



 

 

***************

PersonServiceBean.java

***************

 

[java]  view plain copy
 
 
  1. package blog.service.impl;  
  2.   
  3. import blog.service.PersonService;  
  4.   
  5. public class PersonServiceBean implements PersonService {  
  6.     private String username = null;  
  7.   
  8.     public String getUsername() {  
  9.         return username;  
  10.     }  
  11.   
  12.     public PersonServiceBean() {  
  13.     }  
  14.   
  15.     public PersonServiceBean(String username) {  
  16.         this.username = username;  
  17.     }  
  18.   
  19.     @Override  
  20.     public String save(String name) {  
  21.         if (name.equals("") || name == null) {  
  22.             throw new RuntimeException("出错啦");  
  23.         }  
  24.         System.out.println("in save method!" + name);  
  25.   
  26.         return "in save method!" + name;  
  27.     }  
  28.   
  29.     @Override  
  30.     public String update(String name, Integer userId) {  
  31.         System.out.println("in update method! name = " + name + " id = "  
  32.                 + userId + " username = " + username);  
  33.         return "in update method!name = " + name + " id = " + userId;  
  34.     }  
  35.   
  36.     @Override  
  37.     public String delete(int id, String dept) {  
  38.         System.out.println("id = " + id + " dept = " +  dept);  
  39.         return "in delete method! + nameid = " + id + " dept = " +  dept;  
  40.     }  
  41.   
  42. }  



 

 

***************

SpringAOPTest.java

***************

 

[java]  view plain copy
 
 
  1. package junitTest;  
  2.   
  3.   
  4. import org.junit.BeforeClass;  
  5. import org.junit.Test;  
  6. import org.springframework.context.ApplicationContext;  
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  8.   
  9. import blog.service.PersonService;  
  10.   
  11. public class SpringAOPTest {  
  12.   
  13.     @BeforeClass  
  14.     public static void setUpBeforeClass() throws Exception {  
  15.     }  
  16.       
  17.     @Test  
  18.     public void AopTest(){  
  19.         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
  20.         PersonService service = (PersonService)ctx.getBean("personService");  
  21.         try {  
  22.               
  23.             service.delete(12"it");  
  24.             service.update("发多个地方",324);  
  25.             service.save("");  
  26.               
  27.         } catch (Exception e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  
  31.   
  32. }  



 

 

***************

运行结果

***************

 

 

基于xml配置方式

 

 

*************

beans.xml

************

 

[html]  view plain copy
 
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xsi:schemaLocation="  
  7.            http://www.springframework.org/schema/beans  
  8.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.            http://www.springframework.org/schema/context   
  10.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  11.            http://www.springframework.org/schema/aop  
  12.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  13.            ">  
  14.              
  15.           <aop:aspectj-autoproxy/>  
  16.           <bean id="myInterceptor" class="blog.service.MyInterceptor"/>  
  17.           <bean id="personService" class="blog.service.impl.PersonServiceBean"></bean>  
  18.             
  19.           <aop:config>  
  20.             <aop:aspect id="aspectBean" ref="myInterceptor" >  
  21.                 <aop:pointcut  id="point"  expression="execution (!void blog.service.impl.PersonServiceBean.*(..))"/>  
  22.                 <aop:before method="doAccessCheck" pointcut-ref="point" />  
  23.                 <aop:after-returning method="doAfterReturning" pointcut-ref="point" />  
  24.                 <aop:after-throwing method="doAfterThrowing" pointcut-ref="point"/>  
  25.                 <aop:after method="doAfter" pointcut-ref="point"/>  
  26.                 <aop:around method="doBasicProfiling" pointcut-ref="point" />  
  27.                   
  28.             </aop:aspect>  
  29.           </aop:config>  
  30.             
  31. </beans>  

 

*******************

MyInterceptor.java

*******************

 

[java]  view plain copy
 
 
  1. package blog.service;  
  2.   
  3. import org.aspectj.lang.ProceedingJoinPoint;  
  4.   
  5.   
  6. public class MyInterceptor {  
  7.     public void anyMethod() {  
  8.     }  
  9.   
  10.     public void doAccessCheck() {  
  11.         System.out.println("前置通知" );  
  12.     }  
  13.   
  14.     public void doAfterReturning() {  
  15.         System.out.println("后置通知");  
  16.     }  
  17.   
  18.     public void doAfter() {  
  19.         System.out.println("最终通知");  
  20.     }  
  21.   
  22.     public void doAfterThrowing() {  
  23.         System.out.println("例外通知");  
  24.     }  
  25.       
  26.     public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{  
  27.         //if(){//判断用户是否有权限  
  28.         System.out.println("进入方法");  
  29.         Object result = pjp.proceed();  
  30.         System.out.println("退出方法");  
  31.         //}  
  32.         return result;  
  33.     }  
  34. }  

 

 

aop表达式的使用方法

<aop:pointcut  id="point"  expression="execution (!void blog.service.impl.PersonServiceBean.*(..))"/>

!void blog.service.impl.PersonServiceBean.*(..)中!void 表示拦截所有返回类型为非void类型的方法

java.lang.String表示拦截所有返回类型为String类型的方法

* blog.service.impl.PersonServiceBean.*(java.lang.String,..)表示拦截所有第一个参数为String类型的方法

 

猜你喜欢

转载自m635674608.iteye.com/blog/2309640