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. LCD12864 液晶显示-汉字及自定义显示(串口)

    在网上找了许久,发现FPGA用串口驱动LCD12864程序很少,基本上没有.刚开始窃喜,中间郁闷,最后还是高兴,为什么这样说呢!头一回在没有参考程序的情况下,完全是照时序图写(自信),中间调试过程遇到 ...

  2. mariadb cache1

    http://www.percona.com/blog/2006/07/27/mysql-query-cache/ MySQL Query Cache July 27, 2006 by Peter Z ...

  3. python第三方扩展库及不同类型的测试需安装相对应的第三方库总结

    如何安装第三方库 1.通过python的第三方仓库pypi中查找想要的第三方库 pypi地址:https://pypi.python.org/pypi pip是一个安装和管理Python包的工具,通过 ...

  4. gtk

    GTK官网:www.gtk.org sudo apt-get install build-essential 安装GTK开发套件: sudo apt-get install libgtk2.0-dev ...

  5. unity3d 之本地推送

    1. 本地推送主要包括在android和ios上,下面所有的代码都是本人写的,经过测试是没有问题的,已经运用到项目中了.首先是接口INotification: using System; public ...

  6. [转]ASP.NET Core 1 Deploy to IIS

    本文转自: http://webmodelling.com/webbits/aspnet/aspnet-deploy-iis.aspx 15 Sep 2016. This tutorial will ...

  7. Eclipse 打开js文件时出现 Could not open the editor...

    选择 window-->General-->Editors-->File Associations -->(在右边上面的框中选择jsp或者你打开的文件类型)-->(然后在 ...

  8. [转]解决Maven报错"Plugin execution not covered by lifecycle configuration"

    [转]解决Maven报错"Plugin execution not covered by lifecycle configuration" 导入Myabtis源码后,POM文件会报 ...

  9. FZU 2110 Star

    简单暴力题,读入%lld会WA,%I64d能过. #include<cstdio> #include<cstring> #include<cmath> #inclu ...

  10. memcache和memcached之间的区别和联系

    memcache和memcached区别: Memcache是什么?Memcache是一个自由和开放源代码.高性能.分配的内存对象缓存系统.用于加速动态web应用程序,减轻数据库负载.它可以应对任意多 ...