初识Spring AOP

简介: 初识Spring AOP

概念


AOP介绍


AOP(Aspect-Oriented Programming:面向切面编程)能够将那些与业务无关,却为业务模块所共同调用的逻辑或责任(例如事务处理、日志管理、权限控制等)封装起来,便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可拓展性和可维护性。

Spring AOP 就是基于动态代理的,如果要代理的对象,实现了某个接口,那么 Spring AOP 会使用 JDK Proxy,去创建代理对象,而对于没有实现接口的对象,就无法使用 JDK Proxy 去进行代理了,,这时候 Spring AOP会使用 Cglib 生成一个被代理对象的子类来作为代理,如下图所示:


image.png


除此之外还有一种情况,如果被代理对象实现了接口,可以强制使用 Cglib 实现 AOP。

当然你也可以使用 AspectJ ,Spring AOP 已经集成了AspectJ ,AspectJ 应该算的上是 Java 生态系统中最完整的 AOP 框架了。Spring AOP 基于注解配置的情况下,需要依赖于 AspectJ 包的标准注解,但是不需要额外的编译以及 AspectJ 的织入器,而基于 XML 配置不需要,所以 Spring AOP 只是复用了 AspectJ 的注解,并没有其他依赖 AspectJ 的地方。


使用 AOP 之后我们可以把一些通用功能抽象出来,在需要用到的地方直接使用即可,这样大大简化了代码量。我们需要增加新功能时也方便,这样也提高了系统扩展性。日志功能、事务管理等等场景都用到了 AOP 。


当 Spring 需要使用 @AspectJ 注解支持时,需要在 Spring 配置文件中如下配置:


<aop:aspectj-autoproxy/>
复制代码


而关于强制使用 Cglib,可以通过在 Spring 配置文件如下实现:


<aop:aspectj-autoproxy proxy-target-class="true"/>
复制代码


proxy-target-class 属性值决定是基于接口的还是基于类的代理被创建,默认为 false。如果 proxy-target-class 属性值被设置为 true,那么基于类的代理将有效(这时需要 Cglib 库)。反之是基于接口的代理(JDK的动态代理)。


所以,虽然使用了 AspectJ 的 Annotation,但是并没有使用它的编译器和织入器。其实现原理是 JDK 动态代理或 Cglib,在运行时生成代理类。


AOP核心概念


连接点:JoinPoint,就是要被拦截的方法

切入点:PointCut,定义在哪些类,哪些方法上切入(拦截)

通知:Advice,在拦截到连接点之后要执行的代码,通知主要有五种,前置通知,后置通知,异常通知,最终通知和环绕通知。

切面:AspectJ,就是切入点加上通知

织入:weaving,把切面加入对象,并创建出代理对象的过程。


AOP标签配置元素


aop:aspect: 定义一个切面

aop:before: 定义一个前置通知

aop:after: 定义一个后置通知

aop:around: 定义一个环绕通知

aop:after-returning : 定义一个返回通知

aop:after-throwing :定义一个异常通知

aop:config: 顶层的aop配置元素

aop:pointcut: 定义切入点


AOP实现步骤

  1. 定义普通的业务组件;
  2. 定义切入点;
  3. 定义增强处理,也就是在AOP框架中为普通的业务逻辑织入的处理动作。


实例


基于XML


继续使用租房的案例,接下来新建一个切面类:

public class ProxyXML {
    public void seeHouse(){
        System.out.println("带客户看房子");
    }
    public Object getMoney(ProceedingJoinPoint p){
        System.out.println("租房.....before");
        Object o = null;
        try{
            o = p.proceed();
        }catch(Throwable e){
            e.printStackTrace();
        }
        System.out.println("租房.....after");
        return o;
    }
    public void fare(){
        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: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/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">
    <bean id="host" class="com.msdn.bean.Host" />
    <bean id="proxy" class="com.msdn.aop.ProxyXML" />
    <!--Spring基于Xml的切面-->
    <aop:config>
        <!--定义切点函数-->
        <aop:pointcut id="rentPointCut" expression="execution(* com.msdn.bean.Host.rent())"/>
        <!-- 定义切面 order 定义优先级,值越小优先级越大-->
        <aop:aspect ref="proxy" order="0">
            <!--前置通知-->
            <aop:before method="seeHouse" pointcut-ref="rentPointCut" />
            <!--环绕通知-->
            <aop:around method="getMoney" pointcut-ref="rentPointCut" />
            <!--后置通知-->
            <aop:after method="fare" pointcut-ref="rentPointCut" />
        </aop:aspect>
    </aop:config>
</beans>
复制代码


pom.xml:


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring_study</artifactId>
        <groupId>com.msdn</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>spring-chap3</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.2</version>
        </dependency>
    </dependencies>
</project>
复制代码


测试代码:


@Test
public void aopTest(){
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-aop.xml");
    Object host = context.getBean("host");
    Rent o = (Rent) host;
    o.rent();
}
复制代码


执行结果为:


带客户看房子
租房.....before
房屋出租
租房.....after
收取中介费
复制代码


基于注解


切面类:


@Order(0)
@Aspect
@Component
public class ProxyAnnotation {
    @Before(value = "execution(* com.msdn.bean.Host.rent())")
    public void seeHouse(){
        System.out.println("带客户看房子");
    }
    @Around(value = "execution(* com.msdn.bean.Host.rent())")
    public Object getMoney(ProceedingJoinPoint p){
        System.out.println("租房.....before");
        Object o = null;
        try{
            o = p.proceed();
        }catch(Throwable e){
            e.printStackTrace();
        }
        System.out.println("租房.....after");
        return o;
    }
    @After(value = "execution(* com.msdn.bean.Host.rent())")
    public void fare(){
        System.out.println("收取中介费");
    }
}
复制代码


切面类版本二:


@Order(0)
@Aspect
@Component
public class ProxyAnnotation2 {
    @Pointcut("execution(* com.msdn.bean.Host.rent())")
    public void rentPointCut(){
    }
    @Before(value = "rentPointCut()")
    public void seeHouse(){
        System.out.println("带客户看房子");
    }
    @Around(value = "rentPointCut()")
    public Object getMoney(ProceedingJoinPoint p){
        System.out.println("租房.....before,优先级更高");
        Object o = null;
        try{
            o = p.proceed();
        }catch(Throwable e){
            e.printStackTrace();
        }
        System.out.println("租房.....after");
        return o;
    }
    @After(value = "rentPointCut()")
    public void fare(){
        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: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/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.msdn.bean,com.msdn.aop" />
    <aop:aspectj-autoproxy proxy-target-class="true" />
</beans>
复制代码


测试代码:


@Test
    public void aopTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-aop2.xml");
        Object host = context.getBean("host");
        Rent o = (Rent) host;
        o.rent();
    }
复制代码


执行结果为:


租房.....before
带客户看房子
房屋出租
租房.....after
收取中介费



目录
相关文章
|
29天前
|
监控 Java API
掌握 Spring Boot AOP:使用教程
Spring Boot 中的面向切面编程(AOP)为软件开发提供了一种创新方法,允许开发者将横切关注点与业务逻辑相分离。这不仅提高了代码的复用性和可维护性,而且还降低了程序内部组件之间的耦合度。下面,我们深入探讨如何在 Spring Boot 应用程序中实践 AOP,以及它为项目带来的种种益处。
|
1月前
|
安全 Java Spring
Spring之Aop的底层原理
Spring之Aop的底层原理
|
2天前
|
XML Java 数据格式
Spring使用AOP 的其他方式
Spring使用AOP 的其他方式
12 2
|
2天前
|
XML Java 数据格式
Spring 项目如何使用AOP
Spring 项目如何使用AOP
16 2
|
2天前
|
运维 Java 程序员
Spring5深入浅出篇:基于注解实现的AOP
# Spring5 AOP 深入理解:注解实现 本文介绍了基于注解的AOP编程步骤,包括原始对象、额外功能、切点和组装切面。步骤1-3旨在构建切面,与传统AOP相似。示例代码展示了如何使用`@Around`定义切面和执行逻辑。配置中,通过`@Aspect`和`@Around`注解定义切点,并在Spring配置中启用AOP自动代理。 进一步讨论了切点复用,避免重复代码以提高代码维护性。通过`@Pointcut`定义通用切点表达式,然后在多个通知中引用。此外,解释了AOP底层实现的两种动态代理方式:JDK动态代理和Cglib字节码增强,默认使用JDK,可通过配置切换到Cglib
|
7天前
|
Java 开发者 Spring
Spring AOP的切点是通过使用AspectJ的切点表达式语言来定义的。
【5月更文挑战第1天】Spring AOP的切点是通过使用AspectJ的切点表达式语言来定义的。
20 5
|
7天前
|
XML Java 数据格式
Spring AOP
【5月更文挑战第1天】Spring AOP
24 5
|
7天前
|
Java 编译器 开发者
Spring的AOP理解
Spring的AOP理解
|
8天前
|
XML Java 数据格式
如何在Spring AOP中定义和应用通知?
【4月更文挑战第30天】如何在Spring AOP中定义和应用通知?
14 0
|
8天前
|
安全 Java 开发者
在Spring框架中,IoC和AOP是如何实现的?
【4月更文挑战第30天】在Spring框架中,IoC和AOP是如何实现的?
19 0