欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

注解、原生Spring、SchemaBased三種方式實(shí)現(xiàn)AOP代碼案例

 更新時(shí)間:2023年06月26日 10:58:05   作者:會(huì)洗碗的CV工程師  
這篇文章主要介紹了注解、原生Spring、SchemaBased三種方式實(shí)現(xiàn)AOP的方法介紹,文中有詳細(xì)的代碼示例,對(duì)我們的學(xué)習(xí)有一定的幫助,需要的朋友可以參考下

一、注解配置AOP

Spring可以使用注解代替配置文件配置切面:

1. 開(kāi)啟注解支持

在xml中開(kāi)啟AOP注解支持

以下是bean1.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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.example"></context:component-scan>
    <!-- 開(kāi)啟注解配置Aop -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

2. 在類和方法加入注解

在通知類上方加入注解 @Aspect:配置切面

在通知方法上方加入注解

  • @Before:前置通知
  • @AfterReturning:后置通知
  • @AfterThrowing:異常通知
  • @After:最終通知
  • @Around:環(huán)繞通知

MyAspectAdvice通知類 

package com.example.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyAspectJAdvice {
    // 后置通知
    @AfterReturning("execution(* com.example.dao.UserDao.*(..))")
    public void myAfterReturning(JoinPoint joinPoint){
        System.out.println("切點(diǎn)方法名:"+joinPoint.getSignature().getName());
        System.out.println("目標(biāo)對(duì)象:"+joinPoint.getTarget());
        System.out.println("打印日志···"+joinPoint.getSignature().getName()+"方法被執(zhí)行了!");
    }
    // 前置通知
    @Before("execution(* com.example.dao.UserDao.*(..))")
    public void myBefore(){
        System.out.println("前置通知···");
    }
    // 異常通知
    @AfterThrowing(value = "execution(* com.example.dao.UserDao.*(..))",throwing = "e")
    public void myAfterThrowing(Exception e){
        System.out.println("異常通知···");
        System.out.println(e.getMessage());
    }
    // 最終通知
    @After("execution(* com.example.dao.UserDao.*(..))")
    public void myAfter(){
        System.out.println("最終通知···");
    }
    // 環(huán)繞通知
    @Around("execution(* com.example.dao.UserDao.*(..))")
    public Object myAround(ProceedingJoinPoint point) throws Throwable {
        System.out.println("環(huán)繞前···");
        // 執(zhí)行方法
        Object obj = point.proceed();
        System.out.println("環(huán)繞后···");
        return obj;
    }
}

3. 測(cè)試

測(cè)試方法

    // 測(cè)試注解開(kāi)發(fā)AOP
    @Test
    public void testAdd2(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean1.xml");
        UserDao userDao = (UserDao) ac.getBean("userDao");
        //userDao.update();
        userDao.delete();
    }

測(cè)試結(jié)果(無(wú)異常):

使用update方法測(cè)試結(jié)果(有異常):

可以看到環(huán)繞后沒(méi)有打印,因?yàn)榇藭r(shí)碰到異常終止了程序 

4.  為一個(gè)類下的所有方法統(tǒng)一配置切點(diǎn)

如何為一個(gè)類下的所有方法統(tǒng)一配置切點(diǎn):

在通知類中添加方法配置切點(diǎn)

    // 添加方法配置切點(diǎn)
    @Pointcut("execution(* com.example.dao.UserDao.*(..))")
    public void pointcut(){
    }

在通知方法上使用定義好的切點(diǎn),就是把注解括號(hào)里面得內(nèi)容替換成 "pointCut()" 即可。

二、原生Spring實(shí)現(xiàn)AOP

除了AspectJ,Spring支持原生方式實(shí)現(xiàn)AOP。但是要注意的是原生方式實(shí)現(xiàn)AOP只有四種通知類型:前置通知、后置通知、環(huán)繞通知,異常通知。少了最終通知。

1. 引入依賴

        <!-- AOP -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.3.13</version>
        </dependency>

2. 編寫SpringAOP通知類

Spring原生方式實(shí)現(xiàn)AOP時(shí),只支持四種通知類型:

通知類型實(shí)現(xiàn)接口
前置通知MethodBeforeAdvice
后置通知AfterReturningAdvice
異常通知ThrowsAdvice
環(huán)繞通知MethodInterceptor
package com.example.aspect;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;
import java.lang.reflect.Method;
public class SpringAop implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice, MethodInterceptor {
    /**
     * 環(huán)繞通知
     * @param invocation 目標(biāo)方法
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("環(huán)繞前");
        Object proceed = invocation.proceed();
        System.out.println("環(huán)繞后");
        return proceed;
    }
    /**
     * 后置通知
     * @param returnValue 目標(biāo)方法的返回值
     * @param method 目標(biāo)方法
     * @param args 目標(biāo)方法的參數(shù)列表
     * @param target 目標(biāo)對(duì)象
     * @throws Throwable
     */
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("后置通知");
    }
    /**
     * 前置通知
     * @param method 目標(biāo)方法
     * @param args 目標(biāo)方法的參數(shù)列表
     * @param target 目標(biāo)對(duì)象
     * @throws Throwable
     */
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("前置通知");
    }
    /**
     * 異常通知
     * @param e 異常對(duì)象
     */
    public void afterThrowing(Exception e){
        System.out.println("發(fā)生異常了!");
    }
}

3. 編寫配置類bean2.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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.example"></context:component-scan>
    <!-- 通知對(duì)象 -->
    <bean id="springAop" class="com.example.aspect.SpringAop"/>
    <!-- 配置代理對(duì)象 -->
    <bean id="userDaoProxy"class="org.springframework.aop.framework.ProxyFactoryBean">                    
        <!-- 配置目標(biāo)對(duì)象 -->
        <property name="target" ref="userDao"/>
        <!-- 配置通知 -->
        <property name="interceptorNames">
            <list>
                <value>springAop</value>
            </list>
        </property>
        <!-- 代理對(duì)象的生成方式 true:使用CGLib false:使用原生JDK生成 -->
        <property name="proxyTargetClass" value="true"/>
        <!-- bug -->
        <aop:aspectj-autoproxy proxy-target-class="true"/>
    </bean>
</beans>

4  測(cè)試

測(cè)試類UserDaoTest2

import com.example.dao.UserDao;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class userDaoTest2 {
    // 原生AOP測(cè)試
    @Test
    public void testAdd(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean2.xml");
        UserDao userDao = (UserDao) ac.getBean("userDaoProxy");
        userDao.update();
    }
}

測(cè)試結(jié)果

OK,這里有個(gè)驚喜,如果敲到這里同學(xué),就知道了 

其實(shí)標(biāo)簽?zāi)莻€(gè)bug,是因?yàn)樵椒ㄅ渲妙愐由夏莻€(gè)標(biāo)簽才可以識(shí)別,否則會(huì)報(bào)一個(gè)錯(cuò)誤。

三、SchemaBased實(shí)現(xiàn)AOP

SchemaBased(基礎(chǔ)模式)配置方式是指使用Spring原生方式定義通知,而使用AspectJ框架配置切面。因此這里通知類和上面一樣,看上面的即可。

1. 配置切面

aop3.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        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.example"></context:component-scan>
    <!-- 通知對(duì)象 -->
    <bean id="springAop2" class="com.example.aspect.SpringAop"/>
    <!-- 配置切面 -->
    <aop:config>
        <!-- 配置切點(diǎn) -->
        <aop:pointcut id="myPointcut" expression="execution(* com.example.dao.UserDao.*(..))"/>
        <!-- 配置切面:advice-ref:通知對(duì)象 pointcut-ref:切點(diǎn) -->
        <aop:advisor advice-ref="springAop2" pointcut-ref="myPointcut"/>
    </aop:config>
</beans>

2. 測(cè)試

測(cè)試方法

    // 使用AspectJ框架配置切面測(cè)試
    @Test
    public void t6(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("aop3.xml");
        UserDao userDao = (UserDao) ac.getBean("userDao");
        userDao.add();
    }

測(cè)試結(jié)果

OK,這里的輸出應(yīng)該是我上面定義的不夠完美,有些可能重復(fù)定義了,所以這里就重復(fù)輸出了一些東西 

到此這篇關(guān)于注解、原生Spring、SchemaBased三種方式實(shí)現(xiàn)AOP代碼案例的文章就介紹到這了,更多相關(guān)注解、原生Spring、SchemaBased實(shí)現(xiàn)AOP內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 精通Java接口的使用與原理

    精通Java接口的使用與原理

    接口,在JAVA編程語(yǔ)言中是一個(gè)抽象類型,是抽象方法的集合,接口通常以interface來(lái)聲明。一個(gè)類通過(guò)繼承接口的方式,從而來(lái)繼承接口的抽象方法
    2022-03-03
  • Eclipse IDE可支持Java 14編程

    Eclipse IDE可支持Java 14編程

    這篇文章主要介紹了Eclipse IDE可支持Java 14編程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Java應(yīng)用打包成Docker鏡像

    Java應(yīng)用打包成Docker鏡像

    這篇文章主要為大家介紹了Java應(yīng)用打包成Docker鏡像的過(guò)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • gradle安裝和環(huán)境配置全過(guò)程

    gradle安裝和環(huán)境配置全過(guò)程

    本文介紹了如何安裝和配置Gradle環(huán)境,包括下載Gradle、配置環(huán)境變量、測(cè)試Gradle以及在IntelliJ IDEA中配置Gradle
    2025-01-01
  • springboot實(shí)現(xiàn)訪問(wèn)多個(gè)redis庫(kù)

    springboot實(shí)現(xiàn)訪問(wèn)多個(gè)redis庫(kù)

    這篇文章主要介紹了springboot實(shí)現(xiàn)訪問(wèn)多個(gè)redis庫(kù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • java8如何用Stream查L(zhǎng)ist對(duì)象某屬性是否有重復(fù)

    java8如何用Stream查L(zhǎng)ist對(duì)象某屬性是否有重復(fù)

    這篇文章主要介紹了java8如何用Stream查L(zhǎng)ist對(duì)象某屬性是否有重復(fù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • MyBatis動(dòng)態(tài)SQL表達(dá)式詳解

    MyBatis動(dòng)態(tài)SQL表達(dá)式詳解

    動(dòng)態(tài)SQL可以省略很多拼接SQL的步驟,使用類似于JSTL方式,下面這篇文章主要給大家介紹了關(guān)于Mybatis動(dòng)態(tài)SQL特性的相關(guān)資料,文字通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • SpringBoot中@Autowired爆紅原理分析及解決

    SpringBoot中@Autowired爆紅原理分析及解決

    這篇文章主要介紹了SpringBoot中@Autowired爆紅原理分析及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Spring data jpa @Query update的坑及解決

    Spring data jpa @Query update的坑及解決

    這篇文章主要介紹了Spring data jpa @Query update的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Mybatis 傳參與排序模糊查詢功能實(shí)現(xiàn)

    Mybatis 傳參與排序模糊查詢功能實(shí)現(xiàn)

    這篇文章主要介紹了Mybatis 傳參與排序模糊查詢功能實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2025-04-04

最新評(píng)論