目的:为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添加新方法的更多相关文章

  1. RegisterUserFunc为测试对象添加新方法或重写已有方法

    QTP中为了提高扩展性,提供了一个为测试对象添加一个新的自定义方法,或者重写测试对象已有的方法的函数RegisterUserFunc,在此给大家分享一下. RegisterUserFunc:为测试对象 ...

  2. Spring AOP 切面编程的方法

    spring aop的使用分为两种,一种是使用注解来实现,一种是使用配置文件来实现. 先来简单的介绍一下这两种方法的实现,接下来详细的介绍各处的知识点便于查阅.目录如下: 1.基于注解实现spring ...

  3. Spring AOP无法拦截内部方法调用

    当在同一个类中,A方法调用B方法时,AOP无法工作的问题 假设一个接口里面有两个方法: package demo.long; public interface CustomerService { pu ...

  4. AspectJ之@DeclareParents注解为对象添加新方法

    众所周知,AspectJ可以通过@Before,@After,@Around等注解对连接点进行增强,今天我们来玩一个新注解@DeclareParents.对目标对象增强一个新方法. 场景引入: 现在我 ...

  5. spring aop获取目标对象的方法对象(包括方法上的注解)

    这两天在学习权限控制模块.以前看过传智播客黎活明老师的巴巴运动网视频教程,里面就讲到权限控制的解决方案,当时也只是看看视频,没有动手实践,虽说看过几遍,可是对于系统中的权限控制还是很迷茫,所以借着这次 ...

  6. Spring实例化Bean三种方法:构造器、静态工厂、实例工厂

    Spring中Bean相当于java中的类,可以通过xml文件对bean进行配置和管理. 一.Bean的实例化: 构造器实例化.静态工厂实例化.实例工厂方式实例化. 目录: 构造器实例化: xml配置 ...

  7. 【Java】利用反射执行Spring容器Bean指定的方法,支持多种参数自动调用

    目录 使用情景 目的 实现方式 前提: 思路 核心类 测试方法 源码分享 使用情景 将定时任务录入数据库(这样做的好处是定时任务可视化,也可以动态修改各个任务的执行时间),通过反射执行对应的方法: 配 ...

  8. Groovy中如何向已有的类添加新方法

    Groovy 中有多种途径实现向原有类添加方法,具体有如下几种: MOP(meta object protocol) -- 详见 ExpandoMetaClass 扩展方法 -- GDK采用的此方法 ...

  9. 利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列(46)

    主题:这份代码是开发中常见的代码,查询数据库某个主表的数据,为了提高性能,做一次缓存,每次调用时先拿缓存数据,有则直接返回,没有才向数据库查数据,降低数据库压力. public Merchant lo ...

随机推荐

  1. Error accessing PRODUCT_USER_PROFILE

    1.问题现象再现1)创建用户secSQL> create user sec identified by sec; User created. 2)授权SQL> grant connect, ...

  2. jQuery基础知识--Form基础(续)

    下拉框应用 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF ...

  3. view的onFinishInflate()何时调用的?

    onFinishInflate 当View中所有的子控件均被映射成xml后触发 比如你 自定义一个view叫myView ,路径是,com.test.view.MyView,此view是继承Linea ...

  4. 跨平台移动开发工具:PhoneGap与Titanium全方位比拼

    PhoneGap和Appcelerator Titanium,对于封装和配置移动应用程序而言,二者都是非常受欢迎的开源JavaScript框架.本文为Appcelerator开发者Kevin Whin ...

  5. Android实现button一边圆角一边直角

    http://www.it165.net/pro/html/201503/36211.html

  6. js与C#服务端 json数据交互

    1.1 服务端返回给前端 返回的数据都放入对象中(根据需求:单个对象,集合,键值对),然后JSON序列化返回给前端.这里可以引用JSON.NET 库,也可以用.NET自带的类库: JavaScript ...

  7. 从date中获取相应信息

    创建测试用表: CREATE OR REPLACE VIEW v AS SELECT TO_DATE('2015-5-5 13:14:15', 'YYYY-MM-DD HH24:MI:SS') AS ...

  8. 3 years in Tencent game

    心里一直有继续写博文的愿望,却一直被各种借口打断,现在回头一看,已经在腾讯待了3年半之久.3年半是个比较尴尬的时间点,不好意思说自己是游戏从业老兵,但又觉得自己对于行业已经看到比较清楚了:从当年毕业时 ...

  9. bzoj1013

    这道题题解太多,只贴代码. #include<cstdio> #include<cmath> #include<algorithm> using namespace ...

  10. Wiki动画回顾系列序&&目录

    嘛,前前后后看了太多动画,我自己一直想做的事也是喜欢能做一款acg相关的应用,但一直没有好的点子,当然纠结到最后还是需要一个比较好的社区来让大家加入进来.一直有人让我给他们推番,而我也慢慢懂得“人心” ...