首页 > 其他分享 >什么是Spring AOP里的引入(Introduction)?

什么是Spring AOP里的引入(Introduction)?

时间:2023-06-24 20:12:10浏览次数:50  
标签:Introduction Spring Greeting 接口 AOP 引入 public

在Spring AOP中,引入(Introduction)是一种特殊类型的通知,允许我们向现有的类添加新的接口和实现,而无需修改原始类的代码。引入提供了一种扩展现有类功能的方式,它允许我们在运行时为类动态地添加新的方法和属性。

通过引入,我们可以将新的行为添加到现有的类中,而无需继承该类或修改其代码。这在某些情况下非常有用,特别是当我们不能修改现有类的代码,或者当我们希望将额外的功能与现有类分离时。

引入通过以下两个主要方面来实现:

  1. 接口:引入允许我们在目标对象上实现一个或多个新的接口。这样,目标对象就可以具备新增接口所定义的方法。通过使用接口引入,我们可以在不修改现有类的情况下,为其添加新的行为。

  2. Mixin实现:引入还可以通过Mixin实现来向现有类添加新的方法和属性。Mixin是指在目标对象上实现新的方法和属性,并将其混合(mixin)到目标对象中。这样,目标对象即具有原始类的功能,又具有新增方法和属性的功能。

在Spring AOP中,引入通知使用<introduction>元素进行配置。可以通过XML配置文件或者基于注解的方式来定义引入通知。通过引入,我们可以在不侵入原始类的情况下,为现有的Spring Bean添加新的行为和功能,从而提供更灵活和可扩展的应用程序设计。

Spring AOP使用引入功能的简单示例

首先,我们创建一个接口Greeting,定义了一个sayHello()方法:

public interface Greeting {
    void sayHello();
}

然后,我们创建一个原始类HelloImpl,实现Greeting接口:

public class HelloImpl implements Greeting {
    @Override
    public void sayHello() {
        System.out.println("Hello, World!");
    }
}

接下来,我们使用Spring AOP的引入功能,在不修改HelloImpl类的情况下,为其引入一个新的接口AdditionalFeature,其中定义了一个新的方法additionalMethod()

public interface AdditionalFeature {
    void additionalMethod();
}

public class AdditionalFeatureImpl implements AdditionalFeature {
    @Override
    public void additionalMethod() {
        System.out.println("This is an additional method.");
    }
}

public aspect GreetingIntroductionAspect implements IntroductionInterceptor {
    @DeclareParents(value = "com.example.HelloImpl", defaultImpl = AdditionalFeatureImpl.class)
    private AdditionalFeature additionalFeature;
    
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        if (additionalFeature != null) {
            additionalFeature.additionalMethod();
        }
        return invocation.proceed();
    }
}

在上述代码中,我们创建了一个切面GreetingIntroductionAspect,实现了IntroductionInterceptor接口。通过@DeclareParents注解,我们将AdditionalFeature接口引入到HelloImpl类中,并指定了默认的实现类AdditionalFeatureImpl。在invoke()方法中,我们实现了对新增方法additionalMethod()的调用。

最后,我们使用Spring容器配置文件(例如XML配置或基于注解的配置)将原始类和切面组装在一起:

<bean id="helloBean" class="com.example.HelloImpl"/>

<aop:config>
    <aop:aspect ref="greetingIntroductionAspect">
        <aop:declare-parents types-matching="com.example.Greeting+" implement-interface="com.example.AdditionalFeature"/>
    </aop:aspect>
</aop:config>

现在,我们可以通过获取Greeting接口的实例,并调用sayHello()additionalMethod()两个方法来验证引入功能的效果:

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Greeting greeting = context.getBean(Greeting.class);
        greeting.sayHello();
        
        // 引入的新方法
        AdditionalFeature additionalFeature = (AdditionalFeature) greeting;
        additionalFeature.additionalMethod();
    }
}

当我们运行Main类时,输出结果将是:

Hello, World!
This is an additional method.

这个示例展示了使用Spring AOP的引入功能,在不修改原始类代码的情况下,为其引入了一个新的接口和实现。通过引入功能,我们可以方便地为现有类添加新的行为,提供更灵活和可扩展的应用程序设计。

标签:Introduction,Spring,Greeting,接口,AOP,引入,public
From: https://www.cnblogs.com/shamo89/p/17501628.html

相关文章

  • Spring Cloud 总览表
    SpringCloud总览表微服务技术注册发现远程调用配置管理负载均衡网关路由流量控制系统保护熔断降级服务授权分布式事务TCC模型AT模型Seata缓存技术Redis数据结构SpringDataRedis缓存穿透、雪崩OpenResty多级缓存缓存数据同步Nginx本地缓存数据持......
  • SpringCloud Alibaba入门3之nacos服务搭建
    我们继续在上一章的基础上学习。https://blog.51cto.com/u_13312531/6539601一、下载nacos-server从https://github.com/alibaba/nacos/releasesopeninnewwindow 下载nacos-server发行版。我们使用1.4.2版本二、启动nacos进入%path%\nacos\bin文件夹,执行cmd命令startup.cmd-m......
  • 10. Spring整合
    课程学习到这里,已经对Spring有一个简单的认识了,Spring有一个容器,叫做IoC容器,里面保存bean。在进行企业级开发的时候,其实除了将自己写的类让Spring管理之外,还有一部分重要的工作就是使用第三方的技术。前面已经讲了如何管理第三方bean了,下面结合IoC和DI,整合2个常用......
  • SMU Spring 2023 Contest Round 6
    E.ExpenditureReduction从左右往右找到包含B字符的最近位置,然后从这个位置有从右到左找回去找到包含完所有B字符的位置,这个区间就是答案#include<bits/stdc++.h>#defineinf0x3f3f3f3f#defineendl'\n'#defineintlonglongusingnamespacestd;constintN=......
  • Spring接口简单使用
    Spring接口简单使用ApplicationContextAwareApplicationContextAware是一个Spring接口,用于在Spring应用程序中获取ApplicationContext对象。ApplicationContext是Spring框架对Bean的管理容器,它负责加载、配置和管理应用程序中的Bean。使用ApplicationContextAwar......
  • 微服务 – Spring Cloud – Gateway
    微服务–SpringCloud–GatewayApi网关(ApiGateway)微服务可能分布在不同的主机上,这样有许多缺点:前端需要硬编码调用不同地址的微服务很麻烦;存在跨域访问的问题;微服务地址直接暴露是不安全的。还有所以需要为前端提供一个统一的访问入口。Gateway就是用于解决以上问题的框......
  • Apollo2.1.0+Springboot使用OpenApI
    依赖管理<!--bootstrap最高级启动配置读取--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId><version>3.1.3</v......
  • springcloud项目启动后立即停止
    刚学完springcloud,只看了一遍课没有动手实践,现在第一次自己动手操作的时候不知道该选哪些依赖该怎么配置。依赖全选上后因为各自配置问题运行不起来,只能先从最简单的开始慢慢加依赖。这次只选了nacos和feign,成功运行,项目也没有报错,但是运行后项目立马停止。 百度了一下原因......
  • 8. Java-AOP 面向切面编程
    专题使用汇总:Java-IDEAJava-Maven,依赖管理,私服https://www.cnblogs.com/chenshaojun2008/p/17493632.htmlJava-IOC&DIJava-Mybatis连接池,动态sqlhttps://www.cnblogs.com/chenshaojun2008/p/17496913.htmlJava-文件上传(本地和OSS)Java-登录校验JWT,过滤器,拦截器使用总结......
  • springboot使用Websocket写一个聊天室
     1<!--websocket依赖-->2<dependency>3<groupId>org.springframework.boot</groupId>4<artifactId>spring-boot-starter-websocket</artifactId>5</dependency>目录 ......