pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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模板網!

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

相關文檔推薦

主站蜘蛛池模板: CE认证_FCC认证_CCC认证_MFI认证_UN38.3认证-微测检测 CNAS实验室 | 六维力传感器_三维力传感器_二维力传感器-南京神源生智能科技有限公司 | IHDW_TOSOKU_NEMICON_EHDW系列电子手轮,HC1系列电子手轮-上海莆林电子设备有限公司 | 成都软件开发_OA|ERP|CRM|管理系统定制开发_成都码邻蜀科技 | 福州时代广告制作装饰有限公司-福州广告公司广告牌制作,福州展厅文化墙广告设计, | 洛阳防爆合格证办理-洛阳防爆认证机构-洛阳申请国家防爆合格证-洛阳本安防爆认证代办-洛阳沪南抚防爆电气技术服务有限公司 | TPE_TPE热塑性弹性体_TPE原料价格_TPE材料厂家-惠州市中塑王塑胶制品公司- 中塑王塑胶制品有限公司 | 抖音短视频运营_企业网站建设_网络推广_全网自媒体营销-东莞市凌天信息科技有限公司 | 螺旋叶片_螺旋叶片成型机_绞龙叶片_莱州源泽机械制造有限公司 | 珠光砂保温板-一体化保温板-有釉面发泡陶瓷保温板-杭州一体化建筑材料 | 有机肥设备生产制造厂家,BB掺混肥搅拌机、复合肥设备生产线,有机肥料全部加工设备多少钱,对辊挤压造粒机,有机肥造粒设备 -- 郑州程翔重工机械有限公司 | 合肥宠物店装修_合肥宠物美容院装修_合肥宠物医院设计装修公司-安徽盛世和居装饰 | 重庆钣金加工厂家首页-专业定做监控电视墙_操作台 | 拖链电缆_柔性电缆_伺服电缆_坦克链电缆-深圳市顺电工业电缆有限公司 | 探鸣起名网-品牌起名-英文商标起名-公司命名-企业取名包满意 | 微学堂-电动能源汽车评测_电动车性能分享网 | 成都茶楼装修公司 - 会所设计/KTV装修 - 成都朗煜装饰公司 | RO反渗透设备_厂家_价格_河南郑州江宇环保科技有限公司 | 全屋整木定制-橱柜,家具定制-四川峨眉山龙马木业有限公司 | 防火门-专业生产甲级不锈钢钢质防火门厂家资质齐全-广东恒磊安防设备有限公司 | 球磨机,节能球磨机价格,水泥球磨机厂家,粉煤灰球磨机-吉宏机械制造有限公司 | 定制/定做冲锋衣厂家/公司-订做/订制冲锋衣价格/费用-北京圣达信 | 陕西安玻璃自动感应门-自动重叠门-磁悬浮平开门厂家【捷申达门业】 | 磁力反应釜,高压釜,实验室反应釜,高温高压反应釜-威海自控反应釜有限公司 | 质检报告_CE认证_FCC认证_SRRC认证_PSE认证_第三方检测机构-深圳市环测威检测技术有限公司 | 「钾冰晶石」氟铝酸钾_冰晶石_氟铝酸钠「价格用途」-亚铝氟化物厂家 | 大行程影像测量仪-探针型影像测量仪-增强型影像测量仪|首丰百科 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 | 江苏皓越真空设备有限公司 | Safety light curtain|Belt Sway Switches|Pull Rope Switch|ultrasonic flaw detector-Shandong Zhuoxin Machinery Co., Ltd | 桨叶搅拌机_螺旋挤压/方盒旋切造粒机厂家-无锡市鸿诚输送机械有限公司 | 玻璃钢板-玻璃钢防腐瓦-玻璃钢材料-广东壹诺| 西点培训学校_法式西点培训班_西点师培训_西点蛋糕培训-广州烘趣西点烘焙培训学院 | 阿米巴企业经营-阿米巴咨询管理-阿米巴企业培训-广东键锋企业管理咨询有限公司 | 自动记录数据电子台秤,记忆储存重量电子桌称,设定时间记录电子秤-昆山巨天 | 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | 酒店厨房设计_中央厨房设计_北京商用厨房设计公司-奇能商厨 | 大倾角皮带机-皮带输送机-螺旋输送机-矿用皮带输送机价格厂家-河南坤威机械 | 道康宁消泡剂-瓦克-大川进口消泡剂供应商 | Akribis直线电机_直线模组_力矩电机_直线电机平台|雅科贝思Akribis-杭州摩森机电科技有限公司 | 冷柜风机-冰柜电机-罩极电机-外转子风机-EC直流电机厂家-杭州金久电器有限公司 | 郑州爱婴幼师学校_专业幼师培训_托育师培训_幼儿教育培训学校 |