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. 为什么无论你怎么设置自定义的外层div的高度,easyui的动态添加的tab的高度还是不变高

    由以下代码可知,easyui自动将panel的height设置为了固定的150px高度!

  2. C#调用SCL2008 动态库SCL_API_cdecl.dll or SCL_API_Stdcall.dll,提示找不到指定模块

    在公司用的联想 ThinkPad T430i,原装配置及系统,开机巨慢.按下开机键到登陆界面超过1分钟,再到桌面这个时间就难说了,多时有超过3分钟,进入桌面还要等待几分钟才能正常操作.新年过来向公司领 ...

  3. php 自动发送邮件的实现

    原文: http://www.cnblogs.com/zichi/p/4058055.html http://www.jb51.net/article/32479.htm 效果: 访问: test.p ...

  4. ecos的app包管理

    install 安装 install app_name1 app_name2 有时候要用-o参数,把所有安装过程需要的参数带入进去(安装过程的参数放在app/$app_name/task.php里in ...

  5. hashmap如何初始化

    现在知道的有两种: 1) Map<String, String> hashMap = new HashMap<String, String>(){            {   ...

  6. Java 之final,static小结

    一.final 1.final变量: 当你在类中定义变量时,在其前面加上final关键字,那便是说,这个变量一旦被初始化便不可改变,这里不可改变的意思对基本类型来说是其值不可变,而对于对象变量来说其引 ...

  7. memcached 第二篇----安装使用

    摘要:set add replace get delete gets cas stats 和 flush_all 命令 获取所有key  .你可以使用MemCachedClient的statsItem ...

  8. MyBatis 3 中使用存储过程

    转:http://zachary-guo.iteye.com/blog/1756689 Mybats 是 iBatis 被 Google 收购后重新命名的一个工程,当然也做了大量的升级.iBatis ...

  9. 初步了解php,实现注册及登录

    直接上图(数据库是用wamp实现的) 注册页 html 及 php 代码 登陆页 html 及 php 代码

  10. STM32驱动W25X64存储器

    W25X64 是华邦公司推出的大容量 SPI  FLASH 产品,W25X64 的容量为 64Mbit(8M),该系列还有 W25Q80/16/32 等.W25X16,W25X32,W25X64分别有 ...