最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

注解、原生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. 開啟注解支持

在xml中開啟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>
    <!-- 開啟注解配置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è)試注解開發(fā)AOP
    @Test
    public void testAdd2(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean1.xml");
        UserDao userDao = (UserDao) ac.getBean("userDao");
        //userDao.update();
        userDao.delete();
    }

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

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

可以看到環(huán)繞后沒有打印,因?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)文章

  • Spring Boot配置攔截器及實(shí)現(xiàn)跨域訪問的方法

    Spring Boot配置攔截器及實(shí)現(xiàn)跨域訪問的方法

    這篇文章主要介紹了Spring Boot配置攔截器及實(shí)現(xiàn)跨域訪問的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • Java快速排序及求數(shù)組中第k小的值解析

    Java快速排序及求數(shù)組中第k小的值解析

    這篇文章主要介紹了Java快速排序及求數(shù)組中第k小的值解析,選一個(gè)中間值,把數(shù)組中比它小的元素放到左邊,比它大的元素放到右邊,這時(shí)形成三個(gè)子數(shù)組,分別是中間值,比它大的數(shù)和比它小的數(shù),然后對(duì)前后兩個(gè)數(shù)組進(jìn)行遞歸,需要的朋友可以參考下
    2023-11-11
  • MyBatis使用標(biāo)簽動(dòng)態(tài)操作數(shù)據(jù)庫(kù)詳解

    MyBatis使用標(biāo)簽動(dòng)態(tài)操作數(shù)據(jù)庫(kù)詳解

    這篇文章主要介紹了MyBatis中使用標(biāo)簽動(dòng)態(tài)操作數(shù)據(jù)庫(kù)的方法,動(dòng)態(tài)SQL是指在運(yùn)行PL/SQL塊時(shí)動(dòng)態(tài)輸入SQL語(yǔ)句,是Mybatis的強(qiáng)大特性之?,能夠完成不同條件下不同的sql拼接,需要的朋友可以參考下
    2024-05-05
  • Springboot工程中使用filter過程解析

    Springboot工程中使用filter過程解析

    這篇文章主要介紹了springboot工程中使用filter過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • SpringBoot使用Sa-Token實(shí)現(xiàn)登錄認(rèn)證

    SpringBoot使用Sa-Token實(shí)現(xiàn)登錄認(rèn)證

    本文主要介紹了SpringBoot使用Sa-Token實(shí)現(xiàn)登錄認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Spring Cloud-Feign服務(wù)調(diào)用的問題及處理方法

    Spring Cloud-Feign服務(wù)調(diào)用的問題及處理方法

    Feign 是一個(gè)聲明式的 REST 客戶端,它用了基于接口的注解方式,很方便實(shí)現(xiàn)客戶端配置。接下來通過本文給大家介紹Spring Cloud-Feign服務(wù)調(diào)用,需要的朋友可以參考下
    2021-10-10
  • SpringBoot實(shí)現(xiàn)企業(yè)級(jí)敏感詞攔截檢查系統(tǒng)的設(shè)計(jì)方案

    SpringBoot實(shí)現(xiàn)企業(yè)級(jí)敏感詞攔截檢查系統(tǒng)的設(shè)計(jì)方案

    本文介紹了基于SpringBoot和DFA算法的敏感詞檢測(cè)系統(tǒng)設(shè)計(jì),涵蓋業(yè)務(wù)背景、技術(shù)架構(gòu)、核心模塊、數(shù)據(jù)庫(kù)設(shè)計(jì)、使用示例、監(jiān)控與告警、安全設(shè)計(jì)、部署方案等多個(gè)方面,旨在實(shí)現(xiàn)高效的實(shí)時(shí)敏感詞檢測(cè),保證內(nèi)容合規(guī)性,提升平臺(tái)穩(wěn)定性,需要的朋友可以參考下
    2026-01-01
  • SpringBoot多數(shù)據(jù)源配置詳細(xì)教程(JdbcTemplate、mybatis)

    SpringBoot多數(shù)據(jù)源配置詳細(xì)教程(JdbcTemplate、mybatis)

    這篇文章主要介紹了SpringBoot多數(shù)據(jù)源配置詳細(xì)教程(JdbcTemplate、mybatis),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java實(shí)現(xiàn)隨機(jī)驗(yàn)證碼功能實(shí)例代碼

    Java實(shí)現(xiàn)隨機(jī)驗(yàn)證碼功能實(shí)例代碼

    在這里,我們使用servlet來實(shí)現(xiàn)隨機(jī)驗(yàn)證碼的實(shí)現(xiàn),有需要的朋友可以參考一下
    2013-08-08
  • JAVA JNI原理詳細(xì)介紹及簡(jiǎn)單實(shí)例代碼

    JAVA JNI原理詳細(xì)介紹及簡(jiǎn)單實(shí)例代碼

    這篇文章主要介紹了JAVA JNI原理的相關(guān)資料,這里提供簡(jiǎn)單實(shí)例代碼,需要的朋友可以參考下
    2016-12-12

最新評(píng)論

旬邑县| 伊春市| 武义县| 会同县| 酉阳| 象山县| 正定县| 和平区| 宜川县| 西林县| 绥阳县| 澄江县| 阿荣旗| 无为县| 健康| 合作市| 通河县| 桂林市| 乐都县| 昆明市| 监利县| 屏南县| 石河子市| 商洛市| 福建省| 仪陇县| 张北县| 赫章县| 芷江| 定州市| 徐州市| 永泰县| 紫阳县| 新昌县| 罗甸县| 普兰县| 板桥市| 博野县| 龙井市| 五峰| 湖北省|