Spring 使用原生API介面實現AOP
阿新 • • 發佈:2021-02-15
技術標籤:JavaWebSpringjavaspringaop
文章目錄
一、搭建環境
模擬service
層,首先建立個UserService
介面:
package com.wzq.service;
public interface UserService {
void add();
void delete();
void update();
void query();
}
UserService
package com.wzq.service;
public class UserServiceImpl implements UserService{
public void add() {
System.out.println("增加了一條資訊");
}
public void delete() {
System.out.println("刪除了一條資訊");
}
public void update() {
System.out.println ("更新了一條資訊");
}
public void query() {
System.out.println("查詢了一條資訊");
}
}
在pom.xml
中注入aspectjweaver
依賴:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
<scope>runtime</scope>
</dependency>
二、實現原生API介面
原生API
介面有五個,如下表所示:
這裡實現一下前兩個
建立BeforeLog
類,實現MethodBeforeAdvice
介面,需要寫before
方法,引數說明都在註釋裡:
package com.wzq.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class BeforeLog implements MethodBeforeAdvice {
/*
* method:要執行的目標物件的方法
* args:引數
* target:目標物件
* */
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("執行了" + target.getClass().getName() + ",中的" + method.getName() + "方法");
}
}
建立AfterLog
類,實現AfterReturningAdvice
介面,寫afterReturning
方法:
package com.wzq.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
/*
* returnValue:返回值
* method:要執行的目標物件的方法
* args:引數
* target:目標物件
* */
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("執行了" + target.getClass().getName() +
"中的" + method.getName() + "方法,返回引數是:" + returnValue);
}
}
三、配置xml檔案
在.xml
中配置AOP
需要匯入約束,直接使用下面這個模板即可:
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
首先需要註冊bean
:
<!-- 註冊bean -->
<bean id="userService" class="com.wzq.service.UserServiceImpl"/>
<bean id="afterLog" class="com.wzq.log.AfterLog"/>
<bean id="beforeLog" class="com.wzq.log.BeforeLog"/>
然後是配置aop
:
<!-- 方式一:使用原生Spring API介面 -->
<!-- 配置aop:需要匯入aop約束 -->
<aop:config>
<!-- 切入點 -->
<aop:pointcut id="pointcut" expression="execution(* com.wzq.service.UserServiceImpl.*(..))"/>
<!-- 執行環繞增加 -->
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut" />
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut" />
</aop:config>
注意在aop:pointcut
標籤裡面有一個expression
表示式,它的作用就是切入點入口
expression="execution(修飾符 返回值 包名.類名/介面名.方法名(引數列表))"
在上面的例子中,忽略掉了修飾符,*
表示所有,比如com.wzq.service.UserServiceImpl.*(..))
,它的意思是:com.wzq.service.UserServiceImpl
類下所有的方法和所有的引數
四、測試
編寫測試類:
import com.wzq.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void Test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = (UserService) context.getBean("userService");
service.add();
}
}