Java Web——Spring Aop
阿新 • • 發佈:2018-12-13
面向切面的程式設計——AOP
AOP在Spring中的作用:1.提供宣告式事務 2.允許使用者實現自定義切面 3.不改變原有的程式碼,增加新的功能(其本質通過代理)
註解:不用寫動態代理,簡化開發。將公共業務(日誌、安全、許可權、快取、事務)等與領域業務相結合,使領域業務更加純粹。
專業詞(部分,有時間再補充):
切面:Aspect 連線點:JoinPoint 通知:Advice 織入:Weaving
實現AOP的三種方式
1.使用Spring API實現AOP
- 建立Spring_Aop1工程
- 建立cn.jx.log包,建立Log.java(日誌)
package cn.jx.log; import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; public class Log implements MethodBeforeAdvice { @Override public void before(Method method, Object[]args, Object target) throws Throwable{ System.out.println(target.getClass().getName() + "的" + method.getName() + "方法被執行"); } }
- 業務層示例
- 建cn.jx.service包,包下的UserService.java介面
package cn.jx.service;
public interface UserService {
public void add();
public void update();
public void delete();
public void search();
}
- 建cn.jx.serviceImpl包,包下的UserServiceImpl.java介面
package cn.jx.serviceImpl; import cn.jx.service.UserService; public class UserServiceImpl implements UserService { @Override public void add(){ System.out.println("增加使用者"); } public void update(){ System.out.println("修改使用者"); } public void delete(){ System.out.println("刪除使用者"); } public void search(){ System.out.println("查詢使用者"); } }
- beans.xml配置
- 匯入AOP相關標頭檔案 ,去官方文件找即可
- 建立userServiceImpl和Log之間的關係
- bean標籤之前的全是標頭檔案,沒啥可看的,其實就下邊6行程式碼
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd"
default-lazy-init="true">
<bean id = "userService" class="cn.jx.serviceImpl.UserServiceImpl"/>
<bean id = "Log" class="cn.jx.log.Log"/>
<!--為上面兩個id建立關係,由Spring提供動態代理-->
<aop:config>
<aop:pointcut id="pointuct" expression="execution(* cn.jx.serviceImpl.UserServiceImpl.add())"/>
<aop:advisor advice-ref="Log" pointcut-ref="pointuct"/>
</aop:config>
</beans>
- 寫Test方法,測試類
import cn.jx.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopTest {
@Test
public void aopTest(){
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService) ac.getBean("userService");
userService.add();
}
}