当前位置: 首页 > news >正文

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

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.");
    }
}

 

相关文章:

  • 在resin配置參数实现JConsole远程监控JVM
  • 同样加班 不同收获(转)
  • 使用Java语言开发微信公众平台(八)——自定义菜单功能
  • SPRING-MVC 访问静态资源
  • IE6下position:fixed不支持问题及其解决方式
  • Erlang库 -- 有意思的库汇总
  • 《大数据算法》一2.3 时间亚线性判定算法概述
  • 获取一个数二进制序列中所有的偶数位和奇数位,分别输出二进制序列
  • Setfocus - IE 需要使用setTimeout
  • linux 文件名编码批量转换
  • zabbix监控:监控windows进程
  • CGAL4.10 / CGAL4.13编译
  • multiMap by angular
  • Context源码分析
  • [转]CentO下限制SSH登录次数
  • C# 免费离线人脸识别 2.0 Demo
  • C++11: atomic 头文件
  • Cumulo 的 ClojureScript 模块已经成型
  • Invalidate和postInvalidate的区别
  • JS学习笔记——闭包
  • Less 日常用法
  • Linux快速复制或删除大量小文件
  • Making An Indicator With Pure CSS
  • ReactNativeweexDeviceOne对比
  • Redis 懒删除(lazy free)简史
  • SpriteKit 技巧之添加背景图片
  • vue自定义指令实现v-tap插件
  • Windows Containers 大冒险: 容器网络
  • 从setTimeout-setInterval看JS线程
  • 仿天猫超市收藏抛物线动画工具库
  • 分享一个自己写的基于canvas的原生js图片爆炸插件
  • 开发了一款写作软件(OSX,Windows),附带Electron开发指南
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 三分钟教你同步 Visual Studio Code 设置
  • 深度学习入门:10门免费线上课程推荐
  • 腾讯视频格式如何转换成mp4 将下载的qlv文件转换成mp4的方法
  • 小程序测试方案初探
  • 在 Chrome DevTools 中调试 JavaScript 入门
  • 这几个编码小技巧将令你 PHP 代码更加简洁
  • 积累各种好的链接
  • ​flutter 代码混淆
  • #define与typedef区别
  • (02)Hive SQL编译成MapReduce任务的过程
  • (6)添加vue-cookie
  • (pt可视化)利用torch的make_grid进行张量可视化
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (附源码)SSM环卫人员管理平台 计算机毕设36412
  • (附源码)计算机毕业设计大学生兼职系统
  • (考研湖科大教书匠计算机网络)第一章概述-第五节1:计算机网络体系结构之分层思想和举例
  • (十)c52学习之旅-定时器实验
  • (万字长文)Spring的核心知识尽揽其中
  • (转)Google的Objective-C编码规范
  • (转)Linux NTP配置详解 (Network Time Protocol)
  • (转)真正的中国天气api接口xml,json(求加精) ...
  • .bat批处理(一):@echo off