Java SpringAOP技術之註解方式詳解

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 — 環繞通知(目標對象方法默認不執行的,需要手動執行)

@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("前置通知.............");
    }
    // 環繞通知
    @Around(value = "execution(public void com.qcby.User.add())")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("環繞前置.............");
        //  執行被增強的方法
        proceedingJoinPoint.proceed();
        System.out.println("環繞後置.............");
    }
    // 最終通知
    @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.結果 

總結

本篇文章就到這裡瞭,希望能夠給你帶來幫助,也希望您能夠多多關註WalkonNet的更多內容!   

推薦閱讀: