spring-xml实现aop-通知的种类
生活随笔
收集整理的這篇文章主要介紹了
spring-xml实现aop-通知的种类
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
如果本代碼有疑問,請訪問spring-aop快速入門或者spring-aop動態(tài)代理技術(底層分析)
1.導入aop的相關坐標
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.9.RELEASE</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.13</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.5.RELEASE</version></dependency>2.創(chuàng)建目標接口和目標類
public interface TargetInterface {public void save(); } public class Target implements TargetInterface{public void save() {System.out.println("save running....");} }3.創(chuàng)建切面類
public class MyAspect {public void before(){System.out.println("前置增強...");}public void afterReturning(){System.out.println("后置增強...");}//ProceedingJoinPoint://切入點public Object around(ProceedingJoinPoint point) throws Throwable { System.out.println("環(huán)繞前增強...");//切點方法,spring封裝好的Object proceed = point.proceed();System.out.println("環(huán)繞后增強...");return proceed;} }4.將目標和切面類的對象創(chuàng)建權交給spring
<?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 http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置目標對象--><bean id="target" class="com.hao.aop.Target"/><!-- 切面對象--><bean id="aspect" class="com.hao.aop.MyAspect"></bean> </beans>5.在applicationContext.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/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置目標對象--><bean id="target" class="com.hao.aop.Target"/><!-- 切面對象--><bean id="aspect" class="com.hao.aop.MyAspect"></bean> <!-- 配置織入:告訴spring框架哪些方法需要進行哪些增強(前置增強、后置增強)--><aop:config> <!-- 聲明切面 切面=切點+通知--><aop:aspect ref="aspect"><aop:before method="before" pointcut="execution(public void com.hao.aop.Target.save())"></aop:before><aop:after method="afterReturning" pointcut="execution(public void com.hao.aop.Target.save())"></aop:after><aop:around method="around" pointcut="execution(public void com.hao.aop.Target.save())"></aop:around></aop:aspect></aop:config> </beans>結果:
切點表達式的抽取,為了方便以后更改
<?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 http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 配置目標對象--><bean id="target" class="com.hao.aop.Target"/><!-- 切面對象--><bean id="aspect" class="com.hao.aop.MyAspect"></bean> <!-- 配置織入:告訴spring框架哪些方法需要進行哪些增強(前置增強、后置增強)--><aop:config> <!-- 聲明切面 切面=切點+通知--><aop:aspect ref="aspect"> <!-- 抽取切入點表達式--><aop:pointcut id="myPointcut" expression="execution(public void com.hao.aop.Target.save())"></aop:pointcut><aop:before method="before" pointcut-ref="myPointcut"></aop:before><aop:after method="afterReturning" pointcut-ref="myPointcut"></aop:after><aop:around method="around" pointcut-ref="myPointcut"></aop:around></aop:aspect></aop:config> </beans>總結
以上是生活随笔為你收集整理的spring-xml实现aop-通知的种类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring-基于xml的aop开发-快
- 下一篇: spring-基于注解的aop开发(快速