spring AOP 返回後通知 簡單示例
開始前我們要匯入spring需要的jar包;
還需要引入 aspectjweaver.jar 和 cglib.jar 這兩個jar包
如果是maven專案的話,在pom.xml 的dependencies節點下新增如下引用即可:
Xml程式碼<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程式碼- package org.xmy.ldq.entity;
- import java.io.Serializable;
- /**
- * 測試用實體
-
* @author LiDuanqiang
- */
- public class User implements Serializable{
- private static final long serialVersionUID = 5752197085695030514L;
- private String id;
- private String name;
- private String password;
- private String address;
- public User() {}
-
public User(String id, String name,String password,String address) {
- super();
- this.id = id;
- this.name = name;
- this.password = password;
- this.address = address;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
接下來定義一個通知物件:UserAdvice.java
Java程式碼- package org.xmy.ldq.aop;
- import org.aspectj.lang.JoinPoint;
- /**
- * 使用者操作的通知物件
- * @author LiDuanqiang
- */
- public class UserAdvice {
- /**
- * 主體方法返回後將執行的通知方法
- * @param JoinPoint
- * @param retValue主體方法傳遞到通知方法的返回值
- * @return
- */
- public Object afterReturning(JoinPoint joinPoint,Object retValue)throws Throwable{
- Object object = null;
- object = joinPoint.getThis();//返回代理物件
- /*切入點主體方法的名字*/
- String methodName = joinPoint.getSignature().getName();
- if (retValue instanceof Boolean && (Boolean)retValue && methodName.equals("login")) {
- System.out.println("方法:"+methodName+"()"+"成功執行;"+"登入成功!");
- }
- return object;
- }
- }
再定義一個目標類:AOPBean.java
Java程式碼- package org.xmy.ldq;
- import org.xmy.ldq.entity.User;
- /**
- * 主體Bean
- * @author LiDuanqiang
- */
- public class AOPBean{
- public Boolean login(User user){
- Boolean ret = false;
- /*使用者名稱和密碼都為ldq則成功登入*/
- if (user.getName().equals("ldq")&&user.getPassword().equals("ldq")) {
- ret = true;
- }
- return ret;
- }
- }
最後定義一個測試主程式的實現類:App.java
Java程式碼- package org.xmy.ldq;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.xmy.ldq.entity.User;
- /**
- * 測試
- * @author LiDuanqiang
- */
- public class App{
- public static void main( String[] args ){
- ClassPathXmlApplicationContext factory =
- new ClassPathXmlApplicationContext("applicationContext.xml");
- AOPBean aopBean = (AOPBean) factory.getBean("aopBean");
- aopBean.login(new User("1","ldq","ldq","cs"));
- System.exit(0);
- }
- }
配置檔案很重要:applicationContext.xml
Xml程式碼- <?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"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <aop:config>
- <!-- 定義切點 -->
- <aop:pointcut id="login_PointcutTarget" expression="execution(* org.xmy.ldq.AOPBean.login(..))"/>
- <!-- 定義切面 -->
- <aop:aspect id="userAspect" ref="userAdvice">
- <aop:after-returning
- pointcut-ref="login_PointcutTarget"
- arg-names="joinPoint,retValue"
- returning="retValue"
- method="afterReturning"
- />
- </aop:aspect>
- </aop:config>
- <bean id="userAdvice" class="org.xmy.ldq.aop.UserAdvice"></bean>
- <bean id="aopBean" class="org.xmy.ldq.AOPBean"></bean>
- </beans>
說明:arg-names="joinPoint,retValue" 被切目標方法的引數名稱;
Xml程式碼- returning="retValue" 目標方法成功執行後的返回值 一般型別定義為 Object;
- 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包引入。