Spring aop:decare-parent 为类增加新的方法:

使用XML配置的方式:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="com.stono.sprtest2"></context:component-scan>
<aop:config>
<aop:aspect>
<aop:declare-parents types-matching="com.stono.sprtest2.Singer" implement-interface="com.stono.sprtest2.IntroduceI" default-impl="com.stono.sprtest2.IntroduceImpl" />
<aop:declare-parents types-matching="com.stono.sprtest2.Singer" implement-interface="com.stono.sprtest2.Introduce2I" delegate-ref="intro2" />
</aop:aspect>
</aop:config>
<bean id="intro2" class="com.stono.sprtest2.Introduce2Impl"></bean>
</beans>

AppBean:

package com.stono.sprtest2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppBeans10 {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("appbeans10.xml");
Singer singer = (Singer) context.getBean("singer");
IntroduceI introduceI = (IntroduceI) singer;
introduceI.intro();
Introduce2I introduce2i = (Introduce2I) singer;
introduce2i.intro2();
}
}

Bean:

package com.stono.sprtest2;
import org.springframework.stereotype.Component;
@Component
public class Singer {
public void perform() {
System.out.println("com.stono.sprtest2.Singer.Perform()");
}
}

Interface and Implements:

其中的方法名称不能一致,可能会引起混淆,会把第二个Impl的方法误认为第一个Impl的方法。

package com.stono.sprtest2;
public interface IntroduceI {
void intro();
}
package com.stono.sprtest2;
public class IntroduceImpl implements IntroduceI {
@Override
public void intro() {
System.out.println("this is default introduce!");
}
}
package com.stono.sprtest2;
public interface Introduce2I {
// 这个方法名称不能是intro(),否则可能把这个方法误认为是IntroduceI的方法;
void intro2();
}
package com.stono.sprtest2;
public class Introduce2Impl implements Introduce2I {
@Override
public void intro2() {
System.out.println("this is default introduce2 impl.");
}
}

Spring aop:decare-parent 为类增加新的方法的更多相关文章

  1. spring aop pointcut 切入点是类的公共方法(私有方法不行),还是接口的方法

    spring aop pointcut 切入点是类的公共方法(私有方法不行),还是接口的方法 类的公共方法可以,但是私有方法不行 测试一下接口的方法是否能够捕捉到

  2. win2012 挂载硬盘即增加新硬盘方法

    这篇文章主要介绍了win2012 挂载硬盘即增加新硬盘方法,需要的朋友可以参考下 点击左下角的服务器管理图标 点击右上角的“工具”,再选择“计算机管理” 再点击“磁盘管理” 在磁盘1的按钮处单击右键, ...

  3. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-010-Introduction为类增加新方法@DeclareParents、<aop:declare-parents>

    一. 1.Introduction的作用是给类动态的增加方法 When Spring discovers a bean annotated with @Aspect , it will automat ...

  4. Spring AOP不拦截从对象内部调用的方法原因

    拦截器的实现原理很简单,就是动态代理,实现AOP机制.当外部调用被拦截bean的拦截方法时,可以选择在拦截之前或者之后等条件执行拦截方法之外的逻辑,比如特殊权限验证,参数修正等操作. 但是最近在项目中 ...

  5. 【Spring实战】—— 11 通过AOP为特定的类引入新的功能

    如果有这样一个需求,为一个已知的API添加一个新的功能. 由于是已知的API,我们不能修改其类,只能通过外部包装.但是如果通过之前的AOP前置或后置通知,又不太合理,最简单的办法就是实现某个我们自定义 ...

  6. Spring-AOP为类增加新的功能

    适配器的简单应用实现: 比如:有一个类Car,在类中有两个属性一个为汽车名name,另一个为速度speed.其行为为run(). 现在有一辆车BMWCar 增加了GPS功能.如下实现: 基本类: pu ...

  7. Spring Data Redis 2.x 中 RedisConfiguration 类的新编写方法

    在 Spring Data Redis 1.x 的时候,我们可能会在项目中编写这样一个RedisConfig类: @Configuration @EnableCaching public class ...

  8. spring aop 利用JoinPoint获取参数的值和方法名称

    AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点 ...

  9. spring aop 源码分析(二) 代理方法的执行过程分析

    在上一篇aop源码分析时,我们已经分析了一个bean被代理的详细过程,参考:https://www.cnblogs.com/yangxiaohui227/p/13266014.html 本次主要是分析 ...

随机推荐

  1. javactript关闭窗体,刷新父窗体

    //关闭,父窗口弹出对话框,子窗口直接关闭this.Response.Write("<script language=javascript>window.close();< ...

  2. 省市区三级联动插件:app-jquery-cityselect.js

    (function ($) { $.fn.cityselect = function (options) { var settings = $.extend ({}, options); this.e ...

  3. Oracle存储过程中如何使用游标

    --本存储过程的功能:把test_tbl2中与test_tbl1中ID相同但salary不同的记录中的salary的值更新为test_tbl1中的salary的值--创建存储过程create or r ...

  4. FM收音机 RDS的强大功能

    FM收音机 RDS的强大功能 分类: MTK2011-04-26 16:06 14889人阅读 评论(6) 收藏 举报 交通公告体育音乐娱乐教育 前言 随着发展,会有越来越多的电台具有RDS广播功能, ...

  5. ucos任务优先级从64到256,任务就绪表的改变

    Ucos在任务调度中经常使用的技术为任务就绪表,在之前的文章中使用的例子是低于64个优先级的任务就绪表查找方法,现在ucos将任务扩展到256优先级之后,任务就绪表的查找也做了一定的修改,今天来讲讲 ...

  6. springmvc的jdbcTemplate 插入 返回主键

    public int insertCustomer(final Customer customer) {        //TODO.        final String sql = " ...

  7. 【spoj8222】 Substrings

    http://www.spoj.com/problems/NSUBSTR/ (题目链接) 题意 给出一个字符串S,令${F(x)}$表示S的所有长度为x的子串出现次数的最大值.求${F(1)..... ...

  8. 配置Linux Kernel时make menuconfig执行流程分析

       在编译内核前,一般是根据已有的配置文件(一般在内核根目录下的arch/arm/configs/文件夹下,把该目录下的xxx_defconfig文件拷贝到内核根目录下,并重命名为.config)来 ...

  9. 微信小程序实操-image height:auto问题,url地址报错,“不在以下合法域名列表中”问题等

    1.修改app顶部title 使用API: wx.setNavigationBarTitle({ title: 'titleName'}); 2.ajax请求 wx.request({ url: 'h ...

  10. 关于浏览器和HTTP协议

    关于浏览器 浏览器的主要功能就是向服务器发出请求,在浏览器窗口中展示想要访问的网络资源.这里资源一般是指 HTML 文档,图片等其他的类型.资源的位置由用户使用 URL(统一资源标示符)指定. 而浏览 ...