Spring的IOC和AOP原理及其使用

IOC(要做到编译期不依赖,运行期才依赖)

传统模式 在这里插入图片描述
Spring的处理方式 在这里插入图片描述
采用了工厂模式,降低了类之间的耦合度

基于动态代理增强代码功能,降低了业务模块之间的耦合度,有两种代理方式:

  1. JDK动态代理 (基于接口的动态代理)在这里插入图片描述
  2. cglib动态代理(基于子类的动态代理)
    在这里插入图片描述

AOP

引入依赖:

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.6</version>
</dependency>

5种通知类型理解 在这里插入图片描述
在这里插入图片描述

xml方式:

<?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:context="http://www.springframework.org/schema/context"
       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/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.frank"/>
    <!--配置aop-->
    <aop:config>
        <!--配置切面-->
        <aop:aspect ref="logger">
            <!--配置切入点-->
            <aop:pointcut id="loggerPointcut" expression="execution(* com.frank.service..*(..))"/>
            <!--配置前置通知-->
            <aop:before method="before" pointcut-ref="loggerPointcut"/>
            <!--配置后置通知-->
            <aop:after-returning method="afterReturning" pointcut-ref="loggerPointcut"/>
            <!--配置最终通知-->
            <aop:after method="after" pointcut-ref="loggerPointcut"/>
        </aop:aspect>
    </aop:config>
</beans>

xml+注解方式:

<?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:context="http://www.springframework.org/schema/context"
       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/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.frank"/>
    <!--开启aop注解配置-->
    <aop:aspectj-autoproxy/>
</beans>
package com.frank.advice;

import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class Logger {
    @Pointcut("execution(* com.frank.service..*(..))")
    public void point() {
    }

    @Before("point()")
    public void before() {
        System.out.println("前置通知");
    }

    @AfterReturning("point()")
    public void afterReturning() {
        System.out.println("后置通知");
    }

    @After("point()")
    public void after() {
        System.out.println("最终通知");
    }

}

纯注解配置:

<!--开启aop注解配置-->
<aop:aspectj-autoproxy/>

将上面xml配置改成在主配置类上加@EnableAspectJAutoProxy开启注解支持

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值