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 ...
随机推荐
- replicate-do-db参数引起的MySQL复制错误及处理办法
replicate-do-db配置在MySQL从库的my.cnf文件中,可以指定只复制哪个库的数据.但是这个参数有个问题就是主库如果在其他的schema环境下操作,其binlog不会被从库应用,从而出 ...
- 三 最简单的 AndEngine 程序框架
package com.example.AndEngineExample; import org.anddev.andengine.engine.Engine;import org.anddev.an ...
- JAVA中的成员变量与局部变量
package com.imooc; //1.定义一个类 public class Telphone { //2.属性(成员变量)有什么 float screen; float cpu; float ...
- Python中的sorted函数以及operator.itemgetter函数
operator.itemgetter函数operator模块提供的itemgetter函数用于获取对象的哪些维的数据,参数为一些序号(即需要获取的数据在对象中的序号),下面看例子. a = [1,2 ...
- RDoc
RDoc - Ruby Documentation System home github.com/rdoc/rdoc rdoc docs.seattlerb.org/rdoc bugs github. ...
- [Everyday Mathematics]20150218
设 $A,B$ 是 $n$ 阶复方阵, 适合 $$\bex A^2B+BA^2=2ABA. \eex$$ 试证: 存在 $k\in\bbZ^+$, 使得 $(AB-BA)^k=0$.
- selenium python (一) 开发环境搭建
1.工具下载: python工具共包括三个:python.setuptools.pip ² python:http://python.org/getit/ python开发环境: ² se ...
- xp的虚拟机如何访问本地主机上的文件
1.选中虚拟机,右键选择Settings,在options选项卡里选择Shared Folders,然后在右边设置要共享的文件夹. 2.power on 虚拟机,然后在虚拟机的VM菜单中,选择“Ins ...
- C# 类和结构
类和结构实际上都是创建对象的模板,每个对象都包含数据,并提供了处理和访问数据的方法 . 类定义了类的每个对象(称为实例)可以包含什么数据和功能. 例如,如 果一个类表示一个顾客,就可以定义字段 Cus ...
- 【转】requirejs简单入门
博主今天正式工作啦,工作中用到了js模块化技术,这里转来一个入门教程,很易懂,转给同样刚入门的你们~~ 原地址:http://www.ruanyifeng.com/blog/2012/11/requi ...