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">
<!--開(kāi)啟注解掃描-->
<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 {
//連接點(diǎn)/切入點(diǎn)
public void add(){
System.out.println("add......");
}
}給切面類添加注解 @Aspect,編寫增強(qiáng)的方法,使用通知類型注解聲明
@Component
@Aspect //生成代理對(duì)象
public class UserProxy {
}3.配置文件中開(kāi)啟自動(dòng)代理
<?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">
<!--開(kāi)啟掃描-->
<context:component-scan base-package="com.qcby"></context:component-scan>
<!--開(kāi)啟Aspect生成代理對(duì)象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>4.通知類型注解
@Before -- 前置通知
@AfterReturing -- 后置通知
@Around -- 環(huán)繞通知(目標(biāo)對(duì)象方法默認(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 //生成代理對(duì)象
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ò)了.............");
}
}5.測(cè)試類
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é)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Springcloud Nacos基本操作代碼實(shí)例
這篇文章主要介紹了Springcloud Nacos基本操作代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
java中hashCode方法與equals方法的用法總結(jié)
總的來(lái)說(shuō),Java中的集合(Collection)有兩類,一類是List,再有一類是Set。前者集合內(nèi)的元素是有序的,元素可以重復(fù);后者元素?zé)o序,但元素不可重復(fù)2013-10-10
springboot實(shí)現(xiàn)mock平臺(tái)的示例代碼
本文主要介紹了springboot實(shí)現(xiàn)mock平臺(tái)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
java自動(dòng)根據(jù)文件內(nèi)容的編碼來(lái)讀取避免亂碼
這篇文章主要介紹了java自動(dòng)根據(jù)文件內(nèi)容的編碼來(lái)讀取避免亂碼,需要的朋友可以參考下2014-02-02
對(duì)比Java設(shè)計(jì)模式編程中的狀態(tài)模式和策略模式
這篇文章主要介紹了Java設(shè)計(jì)模式編程中的狀態(tài)模式和策略模式對(duì)比,文中列舉了兩種模式的相似點(diǎn)和不同點(diǎn),并都舉了代碼的實(shí)例作為參照,需要的朋友可以參考下2016-04-04
Kotlin與Java 泛型缺陷和應(yīng)用場(chǎng)景詳解
這篇文章主要為大家介紹了Kotlin與Java 泛型缺陷和應(yīng)用場(chǎng)景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

