您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Java SpringAOP技術(shù)中注解方式是什么”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Java SpringAOP技術(shù)中注解方式是什么”這篇文章吧。
<?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>
package com.qcby; import org.springframework.stereotype.Component; @Component(value = "user") public class User { //連接點(diǎn)/切入點(diǎn) public void add(){ System.out.println("add......"); } }
給切面類添加注解 @Aspect,編寫增強(qiáng)的方法,使用通知類型注解聲明
@Component @Aspect //生成代理對象 public class UserProxy { }
<?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>
@Before -- 前置通知
@AfterReturing -- 后置通知
@Around -- 環(huán)繞通知(目標(biāo)對象方法默認(rèn)不執(zhí)行的,需要手動(dòng)執(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 { //增強(qiáng)/通知 ---》前置通知 @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í)行被增強(qiáng)的方法 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("出錯(cuò)了............."); } }
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(); } }
以上是“Java SpringAOP技術(shù)中注解方式是什么”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。