Java?SpringAOP技術(shù)之注解方式詳解
更新時間:2022年02月23日 11:55:21 作者:C'z?x
這篇文章主要為大家詳細介紹了Java?SpringAOP技術(shù)之注解方式,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
1.配置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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--開啟注解掃描--> <context:component-scan base-package="com.qcby"></context:component-scan> </beans>
2.配置注解
package com.qcby; import org.springframework.stereotype.Component; @Component(value = "user") public class User { //連接點/切入點 public void add(){ System.out.println("add......"); } }
給切面類添加注解 @Aspect,編寫增強的方法,使用通知類型注解聲明
@Component @Aspect //生成代理對象 public class UserProxy { }
3.配置文件中開啟自動代理
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--開啟掃描--> <context:component-scan base-package="com.qcby"></context:component-scan> <!--開啟Aspect生成代理對象--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
4.通知類型注解
@Before -- 前置通知
@AfterReturing -- 后置通知
@Around -- 環(huán)繞通知(目標對象方法默認不執(zhí)行的,需要手動執(zhí)行)
@After -- 最終通知
@AfterThrowing -- 異常拋出通知
package com.qcby; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; @Component @Aspect //生成代理對象 public class UserProxy { //增強/通知 ---》前置通知 @Before(value = "execution(public void com.qcby.User.add())") public void before(){ System.out.println("前置通知............."); } // 環(huán)繞通知 @Around(value = "execution(public void com.qcby.User.add())") public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("環(huán)繞前置............."); // 執(zhí)行被增強的方法 proceedingJoinPoint.proceed(); System.out.println("環(huán)繞后置............."); } // 最終通知 @After(value = "execution(public void com.qcby.User.add())") public void after() { System.out.println("最終通知............."); } //后置通知 @AfterReturning(value = "execution(public void com.qcby.User.add())") public void afterReturning() { System.out.println("后置通知............."); } //異常通知 @AfterThrowing(value = "execution(public void com.qcby.User.add())") public void afterThrowing() { System.out.println("出錯了............."); } }
5.測試類
package com.qcby.test; import com.qcby.User; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserTest { @Test public void aopTest1(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("SpringConfig.xml"); User user = (User) applicationContext.getBean("user"); user.add(); } }
6.結(jié)果
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
java中hashCode方法與equals方法的用法總結(jié)
總的來說,Java中的集合(Collection)有兩類,一類是List,再有一類是Set。前者集合內(nèi)的元素是有序的,元素可以重復(fù);后者元素無序,但元素不可重復(fù)2013-10-10java自動根據(jù)文件內(nèi)容的編碼來讀取避免亂碼
這篇文章主要介紹了java自動根據(jù)文件內(nèi)容的編碼來讀取避免亂碼,需要的朋友可以參考下2014-02-02對比Java設(shè)計模式編程中的狀態(tài)模式和策略模式
這篇文章主要介紹了Java設(shè)計模式編程中的狀態(tài)模式和策略模式對比,文中列舉了兩種模式的相似點和不同點,并都舉了代碼的實例作為參照,需要的朋友可以參考下2016-04-04