spring AOP 返回后通知 简单示例

开始前我们要导入spring需要的jar包;

还需要引入 aspectjweaver.jar 和 cglib.jar 这两个jar包

如果是maven项目的话,在pom.xml 的dependencies节点下添加如下引用即可:

 

Xml代码   收藏代码
  1.   
 

<dependency>

<groupId>aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>1.5.3</version>

</dependency>

<dependency>

<groupId>cglib</groupId>

<artifactId>cglib</artifactId>

<version>2.2.2</version>

</dependency>

现在开始:

 

首先,我们定义一个工程中要用到的简单实体Bean-- User.Java

 

Java代码   收藏代码
  1. package org.xmy.ldq.entity;  
  2. import java.io.Serializable;  
  3. /** 
  4.  * 测试用实体 
  5.  * @author LiDuanqiang 
  6.  */  
  7. public class User implements Serializable{  
  8.     private static final long serialVersionUID = 5752197085695030514L;  
  9.       
  10.     private String id;  
  11.     private String name;  
  12.     private String password;  
  13.     private String address;  
  14.       
  15.     public User() {}  
  16.     public User(String id, String name,String password,String address) {  
  17.         super();  
  18.         this.id = id;  
  19.         this.name = name;  
  20.         this.password = password;  
  21.         this.address = address;  
  22.     }  
  23.     public String getId() {  
  24.         return id;  
  25.     }  
  26.     public void setId(String id) {  
  27.         this.id = id;  
  28.     }  
  29.     public String getName() {  
  30.         return name;  
  31.     }  
  32.     public void setName(String name) {  
  33.         this.name = name;  
  34.     }  
  35.     public String getAddress() {  
  36.         return address;  
  37.     }  
  38.     public void setAddress(String address) {  
  39.         this.address = address;  
  40.     }  
  41.     public String getPassword() {  
  42.         return password;  
  43.     }  
  44.     public void setPassword(String password) {  
  45.         this.password = password;  
  46.     }  
  47.       
  48. }  

 

 接下来定义一个通知对象:UserAdvice.java

 

Java代码   收藏代码
  1. package org.xmy.ldq.aop;  
  2.   
  3. import org.aspectj.lang.JoinPoint;  
  4.   
  5. /** 
  6.  * 用户操作的通知对象 
  7.  * @author LiDuanqiang 
  8.  */  
  9. public class UserAdvice {  
  10.     /** 
  11.      * 主体方法返回后将执行的通知方法 
  12.      * @param JoinPoint  
  13.      * @param retValue主体方法传递到通知方法的返回值 
  14.      * @return 
  15.      */  
  16.     public Object afterReturning(JoinPoint joinPoint,Object retValue)throws Throwable{  
  17.         Object object = null;  
  18.         object = joinPoint.getThis();//返回代理对象  
  19.         /*切入点主体方法的名字*/  
  20.         String methodName = joinPoint.getSignature().getName();  
  21.         if (retValue instanceof Boolean && (Boolean)retValue && methodName.equals("login")) {  
  22.             System.out.println("方法:"+methodName+"()"+"成功执行;"+"登录成功!");  
  23.         }  
  24.         return object;  
  25.     }  
  26. }  

 

 再定义一个目标类:AOPBean.java

 

Java代码   收藏代码
  1. package org.xmy.ldq;  
  2.   
  3. import org.xmy.ldq.entity.User;  
  4.   
  5.   
  6. /** 
  7.  * 主体Bean 
  8.  * @author LiDuanqiang 
  9.  */  
  10. public class AOPBean{  
  11.     public Boolean login(User user){  
  12.         Boolean ret = false;  
  13.         /*用户名和密码都为ldq则成功登录*/  
  14.         if (user.getName().equals("ldq")&&user.getPassword().equals("ldq")) {  
  15.             ret = true;  
  16.         }  
  17.         return ret;  
  18.     }  
  19.       
  20. }  

 

 最后定义一个测试主程序的实现类:App.java

 

Java代码   收藏代码
  1. package org.xmy.ldq;  
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  4. import org.xmy.ldq.entity.User;  
  5.   
  6. /** 
  7.  * 测试 
  8.  * @author LiDuanqiang 
  9.  */  
  10. public class App{  
  11.     public static void main( String[] args ){  
  12.         ClassPathXmlApplicationContext factory =  
  13.             new ClassPathXmlApplicationContext("applicationContext.xml");  
  14.         AOPBean aopBean = (AOPBean) factory.getBean("aopBean");  
  15.         aopBean.login(new User("1","ldq","ldq","cs"));  
  16.         System.exit(0);  
  17.     }  
  18. }  

 

 配置文件很重要:applicationContext.xml

 

Xml代码   收藏代码
  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.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xmlns:p="http://www.springframework.org/schema/p"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  9.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  10.            http://www.springframework.org/schema/context  
  11.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  12.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  13.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  14.     <aop:config>  
  15.         <!-- 定义切点 -->  
  16.         <aop:pointcut id="login_PointcutTarget"  expression="execution(* org.xmy.ldq.AOPBean.login(..))"/>  
  17.         <!-- 定义切面 -->  
  18.         <aop:aspect id="userAspect" ref="userAdvice">  
  19.             <aop:after-returning  
  20.             pointcut-ref="login_PointcutTarget"  
  21.             arg-names="joinPoint,retValue"  
  22.             returning="retValue"  
  23.             method="afterReturning"  
  24.             />  
  25.         </aop:aspect>  
  26.     </aop:config>  
  27.       
  28.     <bean id="userAdvice" class="org.xmy.ldq.aop.UserAdvice"></bean>  
  29.     <bean id="aopBean" class="org.xmy.ldq.AOPBean"></bean>  
  30.       
  31. </beans>  

 

 说明:arg-names="joinPoint,retValue" 被切目标方法的参数名称;

 

Xml代码   收藏代码
  1. returning="retValue" 目标方法成功执行后的返回值 一般类型定义为 Object;  
  2. method="afterReturning"目标方法名称  

 

 

如果程序中报出

java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException

是aspectjweaver.jar包丢失了,将其引入即可;

 Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.错误 是因为采用的CGLIB代理,应把相应的jar包引入。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LiTianao88

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值