久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Spring深入講解實現AOP的三種方式

Spring的AOP就是通過動態代理實現的,使用了兩個動態代理,分別是JDK的動態代理和CGLIB動態代理,本文重點給大家介紹下Spring?Aop的三種實現,感興趣的朋友一起看看吧

[重點] 使用AOP織入 需要導入一個依賴包

  <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.9.1</version>
        </dependency>
    </dependencies>

方式一:使用原生Spring API接口

配置文件

<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--    注冊bean-->
    <bean id="userService" class="com.kero.service.UserServiceImpl"/>
    <bean id="log" class="com.kero.log.Log"/>
    <bean id="afterlLog" class="com.kero.log.AfterLog"/>
<!--    配置aop 需要導入aop的約束-->
    <aop:config>
<!--        切入點 expression表達式 expression(要執行的位置  )-->
        <aop:pointcut id="pointcut" expression="execution(* com.kero.service.UserServiceImpl.*(..))"/>
<!--        執行環繞增加-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterlLog" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

Log

import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
//method:要執行的目標對象的方法
    //objects:參數
    //target:目標對象
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName() + "的" + method.getName() + "被執行了");
    }
}
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
    //method:要執行的目標對象的方法
    //objects:參數
    //target:目標對象
    //returnValue:返回值
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("執行了"+ method.getName() + "方法,返回結果為:"+returnValue);
    }
}

Service

import org.springframework.stereotype.Service;
@Service
public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void search();
}
public class UserServiceImpl implements UserService{
    @Override
    public void add() {
    }
    @Override
    public void delete() {
    }
    @Override
    public void update() {
    }
    @Override
    public void search() {
    }
}

test動態代理 代理的是接口(代理模式是SpringAOP的底層)

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //動態代理 代理的是接口
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }
}

方式二:使用自定義類

配置文件

<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--    注冊bean-->
    <bean id="userService" class="com.kero.service.UserServiceImpl"/>
    <bean id="log" class="com.kero.log.Log"/>
    <bean id="afterlLog" class="com.kero.log.AfterLog"/>
    <bean id="diy" class="com.kero.diy.DiyPointCut"/>
    <aop:config>
<!--        自定義切面 ref要引用的類-->
        <aop:aspect ref="diy">
<!--            切入點-->
            <aop:pointcut id="point" expression="execution(* com.kero.service.UserServiceImpl.*(..))"/>
<!--            通知-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>
</beans>

DIY類

public class DiyPointCut {
    public void before(){
        System.out.println("````方法執行前````");
    }
    public void after(){
        System.out.println("````方法執行后````");
    }
}

其他的不變

方式三:使用注解實現

配置文件

<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--    注冊bean-->
    <bean id="userService" class="com.kero.service.UserServiceImpl"/>
    <bean id="log" class="com.kero.log.Log"/>
    <bean id="afterlLog" class="com.kero.log.AfterLog"/>
<!--    開啟注解支持-->
    <aop:aspectj-autoproxy/>
    <bean id="annotationPointCut" class="com.kero.diy.AnnotationPointCut"/>
</beans>

自定義類

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//使用注解方式實現AOP
@Aspect
//標注這個類是一個切面
public class AnnotationPointCut {
    @Before("execution(* com.kero.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("````方法執行前````");
    }
    @After("execution(* com.kero.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("````方法執行后````");
    }
}

其他不變

補充知識:execution表達式

execution表達式的詳解

切入點表達式:execution(* 包名.*.*(..))

整個表達式可以分為五個部分:

1、execution(): 表達式主體。

2、第一個*號:方法返回類型, *號表示所有的類型。

3、包名:表示需要攔截的包名。

4、第二個*號:表示類名,*號表示所有的類。

5、*(..):最后這個星號表示方法名,*號表示所有的方法,后面( )里面表示方法的參數,兩個句點表示任何參數

其中除了返回類型模式、方法名模式和參數模式外,其它項都是可選的。

舉例:

execution(public * *(..)) 匹配所有的public修飾符的方法

execution(* set*(..)) 匹配所有”set”開頭的方法:

execution(* com.kero.service.UserServiceImpl.*(..))) 匹配UserServiceImpl接口/類的所有方法:

到此這篇關于Spring深入講解實現AOP的三種方式的文章就介紹到這了,更多相關Spring AOP內容請搜索html5模板網以前的文章希望大家以后多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

主站蜘蛛池模板: 成人激情视频免费在线观看 | 毛片久久久 | 成人在线| 日韩欧美国产精品一区二区 | 亚洲国产第一页 | 久久精品91久久久久久再现 | 草久视频 | 爱爱视频网 | 亚洲一区在线观看视频 | 亚洲网视频 | 亚洲在线 | 97av在线| 国产毛片在线看 | 美女在线观看av | 黄色大片视频 | 伊人亚洲| 欧美二级| 国产视频久久 | 日韩欧美一级精品久久 | 国产精品久久久久久婷婷天堂 | 久久久精品在线 | 一区二区免费在线观看 | 中文字幕视频在线 | 亚洲欧美综合精品久久成人 | 午夜天堂精品久久久久 | 国产一区成人 | 国产精品久久久99 | 999免费视频| 亚洲综合色网 | 日韩视频国产 | 中文字幕一区二区三区四区 | 国产精品揄拍一区二区久久国内亚洲精 | 99久久婷婷国产综合精品电影 | www.玖玖玖 | 99九九久久 | 天天天操操操 | 粉嫩一区二区三区四区公司1 | 欧美日韩久久久久 | 久久精品免费观看 | 国产成人精品久久二区二区 | 黄色一级片在线播放 |