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)文章
如何用idea編寫并運行第一個spark scala處理程序
詳細介紹了如何使用IntelliJ IDEA創(chuàng)建Scala項目,包括配置JDK和Scala SDK,添加Maven支持,編輯pom.xml,并創(chuàng)建及運行Scala程序,這為Scala初學者提供了一個基礎(chǔ)的項目搭建和運行指南2024-09-09
springboot啟動feign項目報錯:Service id not legal hostnam的解決
這篇文章主要介紹了springboot啟動feign項目報錯:Service id not legal hostnam的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
java發(fā)送http get請求的兩種方法(總結(jié))
下面小編就為大家?guī)硪黄猨ava發(fā)送http get請求的兩種方法(總結(jié))。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
spring cloud feign實現(xiàn)遠程調(diào)用服務(wù)傳輸文件的方法
這篇文章主要介紹了spring cloud feign實現(xiàn)遠程調(diào)用服務(wù)傳輸文件的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09
MySQL 新增字段但 Java 實體未更新存在潛在問題與解決方案
在 Java + MySQL 的開發(fā)中,我們通常使用 ORM 框架(如 MyBatis、MyBatis-Plus、Hibernate)來映射數(shù)據(jù)庫表與 Java 對象,這篇文章主要介紹了MySQL 新增字段但 Java 實體未更新:潛在問題與解決方案,需要的朋友可以參考下2025-04-04
詳解SpringSecurity如何實現(xiàn)前后端分離
這篇文章主要為大家介紹了詳解SpringSecurity如何實現(xiàn)前后端分離,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03

