那些年、一起追過的Spring--(5)----AOP
阿新 • • 發佈:2019-02-11
瞭解: (在概念上和原理上充分理解)
參考視訊:http://www.jikexueyuan.com/course/665_3.html?ss=1
1. 什麼是AOP
2. AOP的存在價值
3. AOP的原理剖析
4. AOP的關鍵概念
5. AOP的通俗理解
– AOP存在的價值
AOP專門用於處理系統中分佈於各個模組中的交叉關注點的問,在JavaEE應用中,常常通過AOP來處理一些具有橫切性質的系統級服務,如事務管理,安全檢查,快取,物件池管理等,AOP已經成為了一種非常常用的解決方案;
–AOP的原理剖析
AOP代理其實是有AOP框架動態生成的一個物件,該物件可作為目標物件使用,
– AOP的關鍵概念
切面:–Aspect
連線點:–Join Point
通知:–Advice(before,after)
切入點:– Point Cut
引入: – Introduction
目標物件: – Target Object
AOP代理: – AOP Proxy
織入: – Weaving
–AOP通俗理解
一個元件A,不關心其他常用的服務元件B,但是這個元件A使用元件B的時候,不是元件A自身去呼叫,而是通過配置等其他方式,比如Spring中可以通過xml配置檔案。這樣就使的A壓根就不需要知道服務元件B是怎樣的,愛存在不存在,愛怎麼存在都與A無關。A只關心自己的業務邏輯,具體A使用B的時候,配置檔案去做,與具體的A元件無關。
核心概念:
切面,Aspect:其實就是我們需要定義的AOP類,它對功能類似的程式碼進行封裝。
具體實現–XML方式:
導包:
實現程式碼:
package com.spring.aop;
import java.util.Date;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LogAop {
@Before("execution(public void com.spring.dao.impl.StudentDaoImpl.*(..))" )
public void logBefore(){
Date date=new Date();
System.out.println("在方法執行之前執行日誌------");
}
}
package com.spring.dao;
public interface StudentDao {
public void insert();
public void update();
}
package com.spring.dao.impl;
import com.spring.dao.StudentDao;
public class StudentDaoImpl implements StudentDao {
@Override
public void insert() {
System.out.println("method insert");
}
@Override
public void update() {
System.out.println("update method!");
}
}
package com.spring.services;
import com.spring.dao.StudentDao;
public class StudentServices {
private StudentDao dao;
public void setDao(StudentDao dao) {
this.dao = dao;
}
public void insert() {
dao.insert();
}
public void update(){
dao.update();
}
}
package com.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.services.StudentServices;
public class test01 {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("bean-aop-annotation.xml");
StudentServices ss=(StudentServices) context.getBean("studentServices");
ss.insert();
ss.update();
}
}
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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="studentServices" class="com.spring.services.StudentServices">
<property name="dao" ref="studentDao"></property>
</bean>
<bean id="studentDao" class="com.spring.dao.impl.StudentDaoImpl">
</bean>
<bean id="logAop" class="com.spring.aop.LogAop"></bean>
</beans>
執行結果:
在方法執行之前執行日誌------
method insert
在方法執行之前執行日誌------
update method!