第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

spring aop & ioc 容器

標(biāo)簽:
Java

本系列

  1. 初识AOP
  2. JDK的动态代理
  3. CGLIB的动态代理
  4. Spring AOP & JDK动态代理
  5. Spring AOP & CBLIB动态代理

IOC 方式配置AOP

前提:先引入spring-context、cglib、aspectjweaver包

同样的例子 定义一个IHello的接口和一个Hello的实现类如下

package seven.com.seven.aop;

public interface IHello {

    void say();

}

package seven.com.seven.aop;

public class Hello implements IHello {

    @Override
    public void say() {
        System.out.println("hello word");
    }
}

定义一个切面,切面包含 前置通知,后置通知,如下

package com.seven.aop;

import org.aspectj.lang.JoinPoint;

public class Advices {

    public void before(JoinPoint joinPoint){
        System.out.println("--------------advice before--------------");
    }

    public void after(JoinPoint joinPoint){
        System.out.println("--------------advice after--------------");
    }

}

bean文件配置

<?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: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 id="hello" class="com.seven.aop.Hello"/>

    <bean id="advice" class="com.seven.aop.Advices"/>

    <!--AOP 配置
    aop:config  所有AOP的配置都必须配置在这个节点里面
    proxy-target-class:代表被代理的类是否为一个没有实现接口的类
                        true:没有实现接口,使用CGLIB做动态代理
                        false:实现接口,使用JDK做动态代理

    aop:aspect 代表着一个切面,切面的定义不理解可以查看之前文章 <spring aop & jdk的动态代理>
    aop:pointcut 代表着切点
    aop:before 代表前置通知,指明切面要做的事情,因为切点+通知=切面
    aop:after 代表后置通知

    -->
    <aop:config proxy-target-class="false">
        <aop:aspect ref="advice">

            <aop:pointcut id="pointcut" expression="execution(* com.seven.aop.Hello.*(..))"/>

            <aop:before method="before" pointcut-ref="pointcut"/>

            <aop:after method="after" pointcut-ref="pointcut"/>

        </aop:aspect>

    </aop:config>

</beans>

package com.seven.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");
        IHello proxy = (IHello) ctx.getBean("hello");
        proxy.say();
    }
}

运行结果

--------------advice before--------------
hello word
--------------advice after--------------

总结

到这里我们整个AOP系列就结束了,写了六篇文章来讲述其中的知识点,现在做一个简单的梳理

  1. 首先什么是代理模式,为什么需要用动态代理,这是AOP的产生的前提
  2. 关于动态代理,常用的有JDK的动态代理和CGLIB的动态代理,他们的原理是什么,有什么不同
  3. Spring Aop 的一些术语 切点、通知、切面、连接点、织入等是什么意思
  4. Spring Aop 是如何和JDK动态代理、CGLIB动态代理结合的
  5. 工作中可能用的较多的是在Spring IOC 这个容器下使用AOP,其实AOP和Spring IOC容器本身是没有关系的,本篇我们又讲到Spring IOC是如何和AOP结合的

微信公众号:宋坤明
下面的是我的公众号二维码图片,欢迎关注。
图注:宋坤明公众号

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消