Spring Introduction(@DeclareParents)
Spring中,Introduction的实现是通过将需要添加的新的行为逻辑,以新的接口定义增加到目标对象上。在Spring中,spring是通过Before、After、AfterRunning、AfterThrowing以及Around 共5中通知方式为目标方法增加切面功能,比如一个需要在目标类执行一个目标方法之前和之后分别打印一份日志,就可以建立一个切面在这个方法前后打印日志。但是如果我想在此目标类中再增加一个目标方法是,该怎么办呢?
最简单的办法就是在建立此目标类的时候,增加此方法。但是如果原目标类非常复杂,动一发而牵全身。我们可以为需要添加的方法建立一个类,然后建一个代理类,同时代理该类和目标类。如图:
图中,A就是原目标类,B就是新添加的方法所在的类,通过建立一个代理类同时代理A和B,调用者调用该代理时,就可以同时A和B中的方法了。
正好spring已经为我们做好这些事情,我们只需要在spring 下乘凉就可以了,通过@DeclareParents注解就可以实现该功能。下面通过代码来演示
二、代码实例
假设我们的目标类是一个女人,她的核心方法为喜欢帅哥,但是我们又要为该方法添加一个新的功能,建立一个新的雌性类,该类中的方法为非常喜欢吃,把此功能添加到原目标类中。
原目类为(A类)
package com.lzj.spring.annotation;public interface Person {void likePerson();
}
package com.lzj.spring.annotation;
import org.springframework.stereotype.Component;@Component("women")
public class Women implements Person {@Overridepublic void likePerson() {System.out.println("我是女生,我喜欢帅哥");}
}
新添加的类(B类):
package com.lzj.spring.annotation;
import org.springframework.stereotype.Component;public interface Animal {void eat();
}
package com.lzj.spring.annotation;
import org.springframework.stereotype.Component;@Component
public class FemaleAnimal implements Animal {@Overridepublic void eat() {System.out.println("我是雌性,我比雄性更喜欢吃零食");}
}
2、代理类配置
package com.lzj.spring.annotation;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
import org.springframework.stereotype.Component;@Aspect
@Component
public class AspectConfig {//"+"表示person的所有子类;defaultImpl 表示默认需要添加的新的类@DeclareParents(value = "com.lzj.spring.annotation.Person+", defaultImpl = FemaleAnimal.class)public Animal animal;
}
3、Bean的配置类
该类的目的是为了能在spring容器中能注入已装载的Bean.
package com.lzj.spring.annotation;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;@Configuration
@ComponentScan
@EnableAspectJAutoProxy
public class AnnotationConfig {}
4、测试类
package com.lzj.spring.annotation;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class MainTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AnnotationConfig.class);
Person person = (Person) ctx.getBean("women");
person.likePerson();
Animal animal = (Animal)person;
animal.eat();
}
}
出结果为:
我是女生,我喜欢帅哥
我是雌性,我比雄性更喜欢吃零食
Spring Introduction(@DeclareParents)相关推荐
- Spring AOP中declare-parents为特定的类增加新的功能
如果有这样一个需求,为一个已知的API添加一个新的功能. 由于是已知的API,我们不能修改其类,只能通过外部包装.但是如果通过之前的AOP前置或后置通知,又不太合理,最简单的办法就是实现某个我们自定义 ...
- Spring AOP使用教程
AOP 简介 AOP 思想是Spring的核心设计思想之一,通过基于切面的编程设计理念可以将业务逻辑与系统逻辑有效的分隔开来.使得系统的架构更加清晰,模块之间的界限也变的更加明确. AOP 全称为 A ...
- 帝国理工学院的计算机专业怎样,帝国理工学院计算机专业介绍
帝国理工学院也有转专业?是滴,你没有看错!帝国理工学院MSc Computing Science专业适合本科非计算机专业的同学申请.This course provides intensive tra ...
- Spring通过xml文件配置AOP,before、after、around、AfterThrowing、AfterReturning、Declare-parents
1 引包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- Introduction to the Spring Framework By Rod Johnson(完整中文翻译版)
Introduction to the Spring Framework By Rod Johnson 自从这篇文章的第一版在2003年10月发表以来,Spring框架正在逐步普及.经历了1.0最终版 ...
- Spring Boot 1:Introduction
Spring Spring 在不断发展的过程中,边界不断扩张,需要的配置文件也越来越多,使用起来也越复杂,项目中也经常因为配置文件配置错误产生很多问题. 即:Spring 逐渐变成了一个大而全的框架, ...
- 深入聊一聊 Spring AOP 实现机制
点击上方"方志朋",选择"置顶或者星标" 你的关注意义重大! 本文转载于公众号:吉姆餐厅ak 概述 AOP(Aspect-Oriented Programmin ...
- Spring 面向切面编程
AOP,也就是面向方面编程或者说面向面编程,是一种很重要的思想.在企业级系统中经常需要打印日志.事务管理这样针对某一方面的需求,但是传统的面向对象编程无法很好的满足这些需求.因此催生了面向切面编程这样 ...
- 在Intellij上面导入项目 AOP示例项目 AspectJ学习 Spring AoP学习
为了学习这篇文章里面下载的代码:http://www.cnblogs.com/charlesblc/p/6083687.html 需要用Intellij导入一个已有工程.源文件原始内容也可见:link ...
最新文章
- 云计算带来企业管理的革命
- FPGA的配置引脚以及配置过程
- 详细解剖大型H5单页面应用的核心技术点
- 内核中的UDP socket流程(2)——API “sys_socket”
- Windows查杀端口
- 一个硬核分布式数据库公司:pingcap
- php处理heic格式图片,iPhone 照片为heic格式怎么处理?
- 【软考-软件设计师】CPU的功能与组成
- python拷贝文件夹目录和文件_Python 拷贝文件及文件夹到远程主机目录
- Linux 中文无法显示或显示方块
- 我想和iOS大牛们交流的问题
- Linux-nginx安装
- 5号字对应的数字字号_字号对照表
- 设计模式在项目中的应用案例_案例|P6软件在水电项目施工管理中的应用
- ​Python优化机制:常量折叠
- 2021年中国大企业创新百强排行榜:华为位居榜首,北京上榜企业最多(附年榜TOP100详单)
- 一位程序员工作10年总结的10条建议
- oracle学习入门系列之五内存结构、数据库结构、进程
- python f检验 模型拟合度_python 线性回归分析模型检验标准--拟合优度详解
- 苹果xr电池容量_5G iPhone 12电池变小了!外媒:苹果认为5nm工艺很节能