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

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

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

一、注解配置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("切點方法名:"+joinPoint.getSignature().getName());
        System.out.println("目標對象:"+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. 測試

測試方法

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

測試結果(無異常):

使用update方法測試結果(有異常):

可以看到環(huán)繞后沒有打印,因為此時碰到異常終止了程序 

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

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

在通知類中添加方法配置切點

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

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

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

除了AspectJ,Spring支持原生方式實現(xiàn)AOP。但是要注意的是原生方式實現(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原生方式實現(xiàn)AOP時,只支持四種通知類型:

通知類型實現(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 目標方法
     * @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 目標方法的返回值
     * @param method 目標方法
     * @param args 目標方法的參數(shù)列表
     * @param target 目標對象
     * @throws Throwable
     */
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("后置通知");
    }
    /**
     * 前置通知
     * @param method 目標方法
     * @param args 目標方法的參數(shù)列表
     * @param target 目標對象
     * @throws Throwable
     */
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("前置通知");
    }
    /**
     * 異常通知
     * @param e 異常對象
     */
    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>
    <!-- 通知對象 -->
    <bean id="springAop" class="com.example.aspect.SpringAop"/>
    <!-- 配置代理對象 -->
    <bean id="userDaoProxy"class="org.springframework.aop.framework.ProxyFactoryBean">                    
        <!-- 配置目標對象 -->
        <property name="target" ref="userDao"/>
        <!-- 配置通知 -->
        <property name="interceptorNames">
            <list>
                <value>springAop</value>
            </list>
        </property>
        <!-- 代理對象的生成方式 true:使用CGLib false:使用原生JDK生成 -->
        <property name="proxyTargetClass" value="true"/>
        <!-- bug -->
        <aop:aspectj-autoproxy proxy-target-class="true"/>
    </bean>
</beans>

4  測試

測試類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測試
    @Test
    public void testAdd(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean2.xml");
        UserDao userDao = (UserDao) ac.getBean("userDaoProxy");
        userDao.update();
    }
}

測試結果

OK,這里有個驚喜,如果敲到這里同學,就知道了 

其實標簽那個bug,是因為原生方法配置類要加上那個標簽才可以識別,否則會報一個錯誤。

三、SchemaBased實現(xiàn)AOP

SchemaBased(基礎模式)配置方式是指使用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>
    <!-- 通知對象 -->
    <bean id="springAop2" class="com.example.aspect.SpringAop"/>
    <!-- 配置切面 -->
    <aop:config>
        <!-- 配置切點 -->
        <aop:pointcut id="myPointcut" expression="execution(* com.example.dao.UserDao.*(..))"/>
        <!-- 配置切面:advice-ref:通知對象 pointcut-ref:切點 -->
        <aop:advisor advice-ref="springAop2" pointcut-ref="myPointcut"/>
    </aop:config>
</beans>

2. 測試

測試方法

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

測試結果

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

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

相關文章

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

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

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

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

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

    MyBatis使用標簽動態(tài)操作數(shù)據(jù)庫詳解

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

    Springboot工程中使用filter過程解析

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

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

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

    Spring Cloud-Feign服務調用的問題及處理方法

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

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

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

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

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

    Java實現(xiàn)隨機驗證碼功能實例代碼

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

    JAVA JNI原理詳細介紹及簡單實例代碼

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

最新評論

峨边| 梨树县| 东阿县| 蒲江县| 泸西县| 察雅县| 呼玛县| 连平县| 江津市| 巩留县| 镇坪县| 青龙| 南岸区| 舞钢市| 洛阳市| 桃园县| 十堰市| 普洱| 江陵县| 勃利县| 泌阳县| 溧阳市| 永年县| 遂溪县| 来宾市| 南平市| 调兵山市| 信丰县| 双鸭山市| 南陵县| 静乐县| 平南县| 霍邱县| 仁化县| 花垣县| 宁武县| 青海省| 都昌县| 江安县| 清苑县| 增城市|