目的:为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. Java中Volatile关键字详解

    一.基本概念 先补充一下概念:Java并发中的可见性与原子性 可见性: 可见性是一种复杂的属性,因为可见性中的错误总是会违背我们的直觉.通常,我们无法确保执行读操作的线程能适时地看到其他线程写入的值, ...

  2. 統計分析dbms_stats包与analyze 的区别

    Analyze StatementThe ANALYZE statement can be used to gather statistics for a specific table, index ...

  3. Task和BackTask

    一.总结性知识点:     1.Android应用运行时会创建任务Task,用于存放主窗口     2.每一个任务包含一个堆栈数据结构,用于保存当前应用已创建的窗口对象,这个堆栈即回退栈BackSta ...

  4. java-swing在组件中显示信息

    package com.http; import java.awt.*; import javax.swing.*; public class TestSwing2 { //创建了一个能够绘制的组件 ...

  5. 热门Web开发方式 REST实现原理浅析

    REST 首先只是一种架构样式,不是一种标准.这点和 Ajax 类似,两者都是利用现有的成熟技术.在 REST 的定义中,一个 Web 应用总是使用固定的 URI 向外部世界呈现(或者说暴露)一个资源 ...

  6. 数据绑定表达式(上):.NET发现之旅(一)

    数据绑定表达式(上):.NET发现之旅(一) 2009-06-30 10:29:06 来源:网络转载 作者:佚名 共有评论(0)条 浏览次数:859 作为.NET平台软件开发者,我们频繁与各种各样的数 ...

  7. web开发利器 fiddler

    http://mccxj.github.io/blog/20130531_introduce-to-fiddler.html

  8. NewtonPrincipia --- 公理或运动的定律 --- 系理二

    NewtonPrincipia --- 公理或运动的定律 --- 系理二 自然哲学的数学原理>公理或运动的定律>系理II 平行四边形ABCD,那么:直接的力AD由任意的力AB和BD合成,直 ...

  9. 如何解决grails2.3.2中不能运行fork模式

    升级到grails 2.3.2之后,运行时报如下的异常: Exception in thread "main" Error | Forked Grails VM exited wi ...

  10. MVC用户登录方法(lamda表达式)

        public bool ValidateUser(account model) { using (assertEntities db = new assertEntities()) { acc ...