Spring Aop & IOC 容器

本系列
  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结合的

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值