spring AOP Bean添加新方法
目的:为studentAdditionalDetails中添加Student的showDetails()和ExtraShowDetails()两个方法
spring 中AOP能够为现有的方法添加额外的功能,AOP也能为Spring Bean添加新方法
<aop:declare-parents
types-matching="之前原始的类/接口"
implement-interface="想要添加的功能的接口"
defalut-impl="新功能的默认的实现"
/>
或者:
<aop:declare-parents
types-matching="之前原始的类/接口"
implement-interface="想要添加的功能的接口"
delegate-ref="新功能的默认的实现"
/>
配置文件:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 为StudentAdditionalDetails或者StudentAdditionalDetailsImpl添加student里面的方法 -->
<bean id="student" class="imple.StudentImpl">
<property name="studentNo" value="1001" />
<property name="studentName" value="John Peter" />
</bean> <bean id="studentAdditionalDetailsImpl" class="imple.StudentAdditionalDetailsImpl">
<property name="city" value="Newyork" />
<property name="country" value="America" />
</bean> <aop:config>
<aop:aspect>
<!-- types-matching="imple.StudentAdditionalDetailsImpl+" 改成下面的方式 实现的效果相同
说明:types-matching既可以是实现类也可以是接口
-->
<aop:declare-parents types-matching="inter.StudentAdditionalDetails+"
implement-interface="inter.Student"
delegate-ref="student" />
</aop:aspect>
</aop:config> </beans>
或者:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 为StudentAdditionalDetails或者StudentAdditionalDetailsImpl添加student里面的方法 -->
<bean id="student" class="imple.StudentImpl">
<property name="studentNo" value="1001" />
<property name="studentName" value="John Peter" />
</bean> <bean id="studentAdditionalDetails" class="imple.StudentAdditionalDetailsImpl">
<property name="city" value="Newyork" />
<property name="country" value="America" />
</bean> <aop:config>
<aop:aspect>
<!-- types-matching="imple.StudentAdditionalDetailsImpl+" 改成下面的方式 实现的效果相同
说明:types-matching既可以是实现类也可以是接口
-->
<aop:declare-parents types-matching="inter.StudentAdditionalDetails+"
implement-interface="inter.Student"
default-impl="imple.StudentImpl"
/>
</aop:aspect>
</aop:config> </beans>
注意:若采用
default-impl="imple.StudentImpl"
输出结果:不知道为何没有取到值?
1 StudentImpl showDetails studentNo :0
2 StudentImpl showDetails studentName :null
3 StudentImpl ExtraShowDetails studentNo :0
4 StudentImpl ExtraShowDetails studentName :null
5 StudentAdditionalDetailsImpl showAdditionalDetails() city:Newyork
6 StudentAdditionalDetailsImpl showAdditionalDetails() country:America
接口:
package inter;
public interface StudentAdditionalDetails {
public void showAdditionalDetails();
}
package inter;
public interface Student {
public void showDetails();
public void ExtraShowDetails();
}
实现方法:
package imple;
import inter.StudentAdditionalDetails;
public class StudentAdditionalDetailsImpl implements StudentAdditionalDetails {
private String city;
private String country;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public void showAdditionalDetails(){
System.out.println("StudentAdditionalDetailsImpl showAdditionalDetails() city:"+this.city);
System.out.println("StudentAdditionalDetailsImpl showAdditionalDetails() country:"+this.country);
}
}
package imple;
import inter.Student;
public class StudentImpl implements Student {
private int studentNo;
private String studentName;
public int getStudentNo() {
return studentNo;
}
public void setStudentNo(int studentNo) {
this.studentNo = studentNo;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public void showDetails(){
System.out.println("StudentImpl showDetails studentNo :"+this.studentNo);
System.out.println("StudentImpl showDetails studentName :"+this.studentName);
}
public void ExtraShowDetails() {
System.out.println("StudentImpl ExtraShowDetails studentNo :"+this.studentNo);
System.out.println("StudentImpl ExtraShowDetails studentName :"+this.studentName);
}
}
测试方法:
/*
* 基于xml 切面引入性能(为已经实现的类添加新的方法)
* Aop 常见的作用是为现有的方法添加新的功能,该测试方法可以通过Aop为Spring Bean添加新方法
*/
@Test
public void test9(){
StudentAdditionalDetails studentAdditionalDetails = (StudentAdditionalDetails) ctx.getBean("studentAdditionalDetailsImpl");
((Student) studentAdditionalDetails).showDetails();
((Student) studentAdditionalDetails).ExtraShowDetails();
studentAdditionalDetails.showAdditionalDetails();
}
输出结果:
StudentImpl showDetails studentNo :1001
StudentImpl showDetails studentName :John Peter
StudentImpl ExtraShowDetails studentNo :1001
StudentImpl ExtraShowDetails studentName :John Peter
StudentAdditionalDetailsImpl showAdditionalDetails() city:Newyork
StudentAdditionalDetailsImpl showAdditionalDetails() country:America
--------------------------------------------------------以下基于注解实现-----------------------------------------------------------------------------
@DeclareParents(
value=" AspectJ语法类型表达式",
defaultImpl=引入接口的默认实现类)
private Interface interface;
spring AOP Bean添加新方法的更多相关文章
- RegisterUserFunc为测试对象添加新方法或重写已有方法
QTP中为了提高扩展性,提供了一个为测试对象添加一个新的自定义方法,或者重写测试对象已有的方法的函数RegisterUserFunc,在此给大家分享一下. RegisterUserFunc:为测试对象 ...
- Spring AOP 切面编程的方法
spring aop的使用分为两种,一种是使用注解来实现,一种是使用配置文件来实现. 先来简单的介绍一下这两种方法的实现,接下来详细的介绍各处的知识点便于查阅.目录如下: 1.基于注解实现spring ...
- Spring AOP无法拦截内部方法调用
当在同一个类中,A方法调用B方法时,AOP无法工作的问题 假设一个接口里面有两个方法: package demo.long; public interface CustomerService { pu ...
- AspectJ之@DeclareParents注解为对象添加新方法
众所周知,AspectJ可以通过@Before,@After,@Around等注解对连接点进行增强,今天我们来玩一个新注解@DeclareParents.对目标对象增强一个新方法. 场景引入: 现在我 ...
- spring aop获取目标对象的方法对象(包括方法上的注解)
这两天在学习权限控制模块.以前看过传智播客黎活明老师的巴巴运动网视频教程,里面就讲到权限控制的解决方案,当时也只是看看视频,没有动手实践,虽说看过几遍,可是对于系统中的权限控制还是很迷茫,所以借着这次 ...
- Spring实例化Bean三种方法:构造器、静态工厂、实例工厂
Spring中Bean相当于java中的类,可以通过xml文件对bean进行配置和管理. 一.Bean的实例化: 构造器实例化.静态工厂实例化.实例工厂方式实例化. 目录: 构造器实例化: xml配置 ...
- 【Java】利用反射执行Spring容器Bean指定的方法,支持多种参数自动调用
目录 使用情景 目的 实现方式 前提: 思路 核心类 测试方法 源码分享 使用情景 将定时任务录入数据库(这样做的好处是定时任务可视化,也可以动态修改各个任务的执行时间),通过反射执行对应的方法: 配 ...
- Groovy中如何向已有的类添加新方法
Groovy 中有多种途径实现向原有类添加方法,具体有如下几种: MOP(meta object protocol) -- 详见 ExpandoMetaClass 扩展方法 -- GDK采用的此方法 ...
- 利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列(46)
主题:这份代码是开发中常见的代码,查询数据库某个主表的数据,为了提高性能,做一次缓存,每次调用时先拿缓存数据,有则直接返回,没有才向数据库查数据,降低数据库压力. public Merchant lo ...
随机推荐
- POJ 2249 Binomial Showdown
// n 个 数 取 k个数的取法// C(n,k) 注意些细节#include <iostream> #include <string> #include<sstrea ...
- ProgressBar及其子类
1.ProgressBar(进度条组件) 派生了两个常用的组件:SeekBar和RatingBar. <1>通过style属性可以为ProgressBar指定风格,该属性可支持如下几个属性 ...
- Android Traceroute 功能实现
经常在windows下开发网络功能的人 经常会使用的命令就是tracert .而实际上 在app开发中,我们也经常要碰到类似的情况.比如你的app 出现了问题,你总不能让用户想办法 去tracert吧 ...
- 【转】ACE编程小结
转自:http://blog.csdn.net/mjp_mjp/article/details/4406059 1.多线程中的ACE_Reactor::EventLoop,当在多线程(池)中调用Eve ...
- FastJson只序列化java对象的部分属性
public class Student { private int id; private String name; private int age; //get set方法略 } 如下方法: St ...
- 组合 z
输入a b c d e以及它们对应的数字 比如 a-->1 2 3 b-->2 3 c-->1 d-->3 4 5 e-->1 3 5 输出a b c d e的可用组合 ...
- linux实现自动远程备份(scp+ssh)
刚上线的服务器需要备份日志,要备份到另一台服务器上去,为了减少工作量,采用linux的定时任务去自动执行.因服务器都是linux的,因此采用linux的远程复制scp命令.但这里涉及到一个问题,就是s ...
- C# 类和结构
类和结构实际上都是创建对象的模板,每个对象都包含数据,并提供了处理和访问数据的方法 . 类定义了类的每个对象(称为实例)可以包含什么数据和功能. 例如,如 果一个类表示一个顾客,就可以定义字段 Cus ...
- 前端面试题(JS篇)
原题地址:http://handyxuefeng.blog.163.com/blog/static/454521722013111714040259/ 好吧,最近打算换工作,所以关注比较多的是面试题, ...
- Guidelines for clock
用两个256x16的基本存储器构成512x16的数据存储器,因为256x16的基本存储器读写时序不太符合MCU的要求,于是改写之.利用下降沿控制输入,作为基本存储器控制时钟,而上升沿控制数据输出寄存器 ...