當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring学习11之AOP
生活随笔
收集整理的這篇文章主要介紹了
Spring学习11之AOP
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、AOP是什么?
AOP(Aspect Oriented Programming),即面向切面編程,可以說是OOP(Object Oriented Programming,面向對象編程)的補充和完善。
AOP核心概念
- 橫切關注點:跨越應用程序多個模塊的方法或功能。即是,與我們業務邏輯無關的,但是我們需要關注的部分,就是橫切關注點。如日志,安全,緩存,事務等等…
- 切面(ASPECT):橫切關注點被模塊化的特殊對象。即,它是一個類。
- 通知(Advice):切面必須要完成的工作。即,它是類中的一個方法。
- 目標(Target):被通知對象。
- 代理(Proxy) :向目標對象應用通知之后創建的對象。
- 切入點(Pointcut) :切面通知執行的“地點"的定義
- 連接點(JointPoint):與切入點匹配的執行點。
二、代碼
1.需要的類
業務的接口
package com.shan.demo04;public interface UserService {void add();void delete();void update();void query();}業務的實現類
package com.shan.demo02; //改變業務代碼在公司是大忌,因為你改動代碼可能讓整個項目崩潰 public class UserServiceImpl implements UserService{@Overridepublic void add() {System.out.println("增加了一個用戶");}@Overridepublic void delete() {System.out.println("刪除了一個用戶");}@Overridepublic void update() {System.out.println("修改了一個用戶");}@Overridepublic void query() {System.out.println("查詢了一個用戶");} }前置日志類
package com.shan.log;import org.springframework.aop.MethodBeforeAdvice;import java.lang.reflect.Method;public class BeforeLog implements MethodBeforeAdvice {//method:要執行的目標對象的方法// Object[]:參數// target:目標對象public void before(Method method, Object[] args, Object target) throws Throwable {System.out.println(target.getClass().getName()+"的"+method.getName()+"被執行了!");} }后置日志類
package com.shan.log;import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;public class AfterLog implements AfterReturningAdvice {//returnValue:返回值//method:要執行的目標對象的方法// Object[]:參數// target:目標對象public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {System.out.println("執行了!"+target.getClass().getName()+"的"+method.getName()+"方法.返回結果為:"+returnValue);} }Spring 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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--注冊bean--><bean id="userService" class="com.shan.service.UserServiceImpl"/><bean id="beforeLog" class="com.shan.log.BeforeLog"/><bean id="afterLog" class="com.shan.log.AfterLog"/><!--配置aop;需要導入aop的約束--><aop:config><!--切入點:expression:表達式,execution(要執行的位置)--><aop:pointcut id="pointcut" expression="execution(* com.shan.service.UserServiceImpl.*(..))"/><!--執行環繞增加--><aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/><aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/></aop:config></beans>2.測試
import com.shan.service.UserService; import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//動態代理 代理的是接口UserService userService = context.getBean("userService", UserService.class);userService.add();} }com.shan.service.UserServiceImpl的add被執行了!
增加了一個用戶
執行了!com.shan.service.UserServiceImpl的add方法.返回結果為:null
總結
動態代理 代理的是接口
配置aop,需要導入aop的約束
xmlns:aop="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
切入點:expression:表達式,execution(要執行的位置)
作者有話說
博客創作不易,希望看到這里的讀者動動你的小手點個贊,如果喜歡的小伙伴可以一鍵三連,作者大大在這里給大家謝謝了。
總結
以上是生活随笔為你收集整理的Spring学习11之AOP的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring学习9之静态代理再理解
- 下一篇: Spring学习10之动态代理