spring-4-aop

AOP简介

image-20201128211246682

aop使得 : 紧耦合 -> 松耦合 ,通过配置。

目标 + 增强 目前认为就是切面。

image-20201128211853179

image-20201128211900184

1.4

jdk要接口。

image-20201128212403700

1.5 JDK的动态代理

//要能看懂

image-20201128213700338

1.6 cglib的动态代理

spring-core已经集成了cglib

image-20201128214630982

1.7 AOP相关概念

可以被增强的方法叫连接点。

通知 是增强的方法。

切面:

织入:目标方法 和 增强方法 结合的过程

image-20201128215242758

image-20201129131143940

image-20201129131218023

基于XML的AOP

2.1快速入门

image-20201129131306761

aop是一种思想,spring内部实现了,但spring觉得aspectj的要更好,不排斥他,建议大家用aspectj实现的aop

//配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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="target" class="com.ustc.aop.Target">bean>
    
    <bean id="aspect" class="com.ustc.aop.MyAspect">bean>
    
    <aop:config>
    	//声明切面
        <aop:aspect ref="aspect">
        //切面 = 切点 + 通知
            <aop:before method="before" 
            //切点表达式
            pointcut="execution(public void com.ustc.aop.Target.save())"/>
        aop:aspect>
    aop:config>

beans>
//测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TargetTest {
    @Autowired
    private TargetInterface mTarget;

    @Test
    public void test(){
        mTarget.save();
    }

}
<<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-testartifactId>
    <version>5.0.5.RELEASEversion>
dependency>

spring的很多功能都要把bean放到容器当中。

2.2 XML配置AOP详解

切点表达式的写法

  • (..) 表示任意参数

image-20201129141311288

image-20201129141525590

image-20201129141832732

image-20201129142531475

image-20201129142615311

基于注解的AOP开发

3.1快速入门

image-20201129143234738

组件扫描得用到context命名空间
//切面类
@Component
@Aspect  //标注当前类是一个切面类
public class MyAspect {

    @Before("execution(* com.ustc.anno.Target.*(..))")
    public void before(){
        System.out.println("前置增强");
    }
}
//目标类
@Component
public class Target implements TargetInterface {
    public void save() {
        System.out.println("save running");
    }
}
//配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.ustc.anno"/>

    <aop:aspectj-autoproxy/>
beans>
//测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-anno.xml")
public class AopAnnoTest {
    @Autowired
    private TargetInterface mTargetInterface;

    @Test
    public void test(){
          mTargetInterface.save();
    }

}

3.2详解

image-20201129145535745

image-20201129151051350

image-20201129151221929

  • 先搞明白xml方式,搞明白概念。

  • 要让spring知道是切面类,需要@Aspect 注解


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!