spring AOP

spring AOP学习笔记


Aop思想

Aop是spring几个重要的思想之一,在面试的时候也常常问到。所谓的Aop指的是面向切面编程。一个大的系统必然会分成几个模块。每个模块负责完成系统的一项功能。在每个模块中有一些通用的辅助功能模块,比如日志管理,安全管理等。然而这些通用的辅助功能模块导致系统管理起来非常负责。所以我们需要考虑是不是可以将这些通用的辅助模块从每个功能模块中抽取出来进行统一的管理。Aop帮助我们实现了这一点。无需修改功能模块的代码。
spring 为我们提供了很好的Aop的集成,我们只需要进行简单的配置就可以实现Aop。Aop内部的实现原理是动态代理。在动态代理学习模块对动态代理有详细的描述。

动态代理

代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息、过滤消息、把消息转发给委托类,以及事后处理消息等。代理类与委托类之间通常会存在关联关系,一个代理类的对象与一个委托类的对象关联,代理类的对象本身并不真正实现服务,而是通过调用委托类的对象的相关方法,来提供特定的服务。
按照代理的创建时期,代理类可以分为两种。
静态代理:由程序员创建或特定工具自动生成源代码,再对其编译。在程序运行前,代理类的.class文件就已经存在了。
动态代理:在程序运行时,运用反射机制动态创建而成。


spring中配置AOP

spring提供两种配置AOP的模式,一种是通过xml进行配置,一种是通过Annotation。下面来看看这两种配置方式

xml文档配置方式

代理类方法

package com.zhouqi.aop;

public class Lio implements Performer{

    private Instrument guitar;
    public Instrument getGuitar() {
        return guitar;
    }

    public void setGuitar(Instrument guitar) {
        this.guitar = guitar;
    }

    @Override
    public void perform(){
        guitar.play();
    }

}

如上所示就是代理类,一个叫lio的演奏者实现了performer接口,其中performer接口中仅含有一个方法就是perform()。我们想要在perform方法调用之前或者之后调用customer这一委托类的方法。也就是在表演者表演之前观众找座位坐下,当表演结束之后观众离开。所以在调用perform()方法之前我们要织入customer的takeSeat()方法,在之后要织入leave()方法。

package com.zhouqi.aop;

public class Customer {

    public void takeSeat(){
        System.out.println("the customer is taking seat...");
    }

    public void leave(){
        System.out.println("The customers are leaving");
    }

}

在定义好代理类和委托类之后我们就要在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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="guitar" class="com.zhouqi.aop.Guitar"></bean>
    <bean id="lio" class="com.zhouqi.aop.Lio">
        <property name="guitar" ref="guitar"></property>
    </bean>
    <bean id="customer" class="com.zhouqi.aop.Customer"></bean>
    <aop:config>
        <aop:aspect id="customerAsp" ref="customer">
            <aop:pointcut id="customerPerf"
                        expression="execution(*    com.zhouqi.aop.Performer.perform(..))"/>
            <aop:before method="takeSeat" pointcut-ref="customerPerf"/>
            <aop:after method="leave" pointcut-ref="customerPerf"/>
        </aop:aspect>
    </aop:config>
</beans>

如上所示就是在xml文档中配置spring的例子。需要注意,在使用aop是要在xml文档中加上
xmlns:aop=”http://www.springframework.org/schema/aop”
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd

之后才能对aop进行配置。

使用annotation进行配置

基于annotation的配置更加地简介明了,但是对于aop来说用的较多的是使用xml文档进行配置。因为如果我们使用的是其他人所提供的方法,看不到源码,就不能在源码上加上annotation。所以xml用的较多。使用annotation需要在代理类和委托类中加入如下的注释。

package com.zhouqi.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class Customer {

    @Before("execution(* com.zhouqi.aop.Performer.perform(..))")
    public void takeSeat(){
        System.out.println("the customer is taking seat...");
    }

    @After("execution(* com.zhouqi.aop.Performer.perform(..))")
    public void leave(){
        System.out.println("The customers are leaving");
    }

}

package com.zhouqi.aop;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Lio implements Performer{

    @Autowired
    private Instrument guitar;
    public Instrument getGuitar() {
        return guitar;
    }

    public void setGuitar(Instrument guitar) {
        this.guitar = guitar;
    }

    @Override
    public void perform(){
        guitar.play();
    }

}

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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/tx
        http://www.springframework.org/schema/tx/spring-tx.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.zhouqi.aop"/>
     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

在xml文档中加入

<context:component-scan base-package="com.zhouqi.aop"/>

表明对包com.zhouqi.aop中所有标注有@component等标注的类进行扫描并纳入到spring容器的管理之中。

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

加入上述内容表明,其将在spring上下文中自动创建一个AnnotationAwareAspectJAutoProxyCreator的类,它会自动代理一些bean。

通过上述两种在spring中配置aop我们已经完成了aop的配置,现在进行测试。

package com.zhouqi.aop;

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

public class TestAop {
    public static void main(String[] args){
        ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        Performer p = (Performer)ac.getBean("lio");
        p.perform();
    }
}

我们可以得到如下的结果

the customer is taking seat…
The guitar is playing
The customers are leaving

传递参数给所标注的通知

有时候 需要将代理类方法中的参数传递给委托类。这是需要进行特殊的标注

package com.zhouqi.aop;

import org.springframework.stereotype.Component;

@Component("thinker")
public class Thinker {
    public void thinkOfSomething(String mind){
        System.out.println("the thinker is thingking");
    }
}

如上所示,假如有一个代理类Thinker。它有一个方法thinkofSomething(String mind),需要将mind传递给委托类,那么Advicor应该如何配置呢?

package com.zhouqi.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class Magician implements MindReader{
    private String mind;
    public void getMind() {
        if(mind ==null){
            return;
        }
        System.out.println(mind);
    }
    @Before("execution(* com.zhouqi.aop.Thinker.thinkOfSomething(String)) && args(mind)")
    public void interceptMind(String mind) {
        System.out.println("the mind has been intercept:"+ mind);
        this.mind = mind;
    }

如上所示是一个委托类Magician , 它可以读取Thinker的思想。

所需要的jar包

在进行spring配置的时候经常会报错,报错的原因很多情况下是缺少相应的jar包
下图是我在进行实验室根据提示添加的jar包,添加完之后可以正常运行。
spring aop所需要的jar包


总结

spring aop是对面向对象思想的一个重要的补充。有助于降低模块与模块之间的耦合。减少模块中与主要逻辑无关的代码,使得代码更加简洁清晰。spring的主要思想有DI、IOC和AOP。在接下来的时间需要进一步研究spring相关的源码。

###Spring AOP 的概念 AOP(Aspect-Oriented Programming)即面向切面编程,是一种编程范式,旨在通过分离横切关注点来提高模块化程度。在 Spring 框架中,AOP 被广泛用于实现诸如日志记录、事务管理、安全性等通用功能,这些功能通常与业务逻辑无关但又需要在多个地方重复使用。 Spring AOP 主要是基于 AspectJ 实现的,尽管 AspectJ 是一个独立的 AOP 框架,并不是 Spring 的组成部分,但它通常与 Spring 一起使用以提供更强大的 AOP 功能[^1]。Spring AOP 支持两种方式来定义切面:基于 XML 配置文件的方式和基于注解的方式。 ###Spring AOP 的原理 Spring AOP 使用运行时代理来实现 AOP 功能,这意味着它会在运行时动态生成代理对象。对于实现了接口的类,Spring AOP 默认使用 JDK 动态代理;而对于没有实现接口的类,则会使用 CGLIB 代理[^4]。这种方式允许在不修改原始代码的情况下向程序中添加新的行为。 织入(Weaving)是将增强(advice)应用到目标对象的过程,Spring AOP 在运行时进行织入操作[^3]。当创建了代理对象后,所有对目标对象方法的调用都会被拦截,并且可以插入额外的操作,比如在方法执行前后做一些处理。 ###Spring AOP 的使用教程 要开始使用 Spring AOP,首先需要确保项目中包含了必要的依赖。如果使用 Maven 构建工具,可以在 `pom.xml` 文件中加入如下依赖: ```xml <!-- 引入aop依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ``` 一旦添加了依赖并刷新了 Maven 项目,就可以开始编写切面了。下面是一个简单的例子,展示如何使用注解来定义一个切面: ```java import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void logBefore(JoinPoint joinPoint) { System.out.println("Method " + joinPoint.getSignature().getName() + " is called."); } } ``` 在这个示例中,`LoggingAspect` 类被标记为 `@Aspect` 和 `@Component` 注解,这样 Spring 就能识别这是一个切面组件。`@Before` 注解指定了在哪些方法上应用前置通知(before advice),这里的表达式表示匹配 `com.example.service` 包下所有的方法。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值