Spring @Aspect进行类的接口扩展:

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">
<!-- 这个声明会创建AnnotationAwareAspectJAutoProxyCreator,进行切面Bean的代理 -->
<aop:aspectj-autoproxy />
<!-- 必须将切面类声明为一个Bean -->
<bean id="introduceAspect" class="com.stono.sprtest3.IntroduceAspect"></bean>
<bean id="singer" class="com.stono.sprtest3.Singer"></bean>
</beans>

AppBean:

package com.stono.sprtest3;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppBeans13 {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("appbeans13.xml");
// 有接口必须使用接口,使用Singer类会出现ClassCastException;
Performer bean = (Performer) context.getBean("singer");
IntroduceI introduceI = (IntroduceI) bean;
introduceI.introduce();
}
}

Aspect:

package com.stono.sprtest3;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
@Aspect
public class IntroduceAspect {
// 为Performer接口扩展增加IntroduceI接口;
@DeclareParents(value = "com.stono.sprtest3.Performer+", defaultImpl = DefaultIntro.class)
public static IntroduceI introduceI;
}

POJO:

package com.stono.sprtest3;
public interface Performer {
void perform();
}
package com.stono.sprtest3;
public class Singer implements Performer {
public void perform() {
System.out.println("com.stono.sprtest3.Singer.perform()");
}
}
package com.stono.sprtest3;
public interface IntroduceI {
void introduce();
} package com.stono.sprtest3;
public class DefaultIntro implements IntroduceI {
@Override
public void introduce() {
System.out.println("this is default introduce");
}
}

Spring @Aspect进行类的接口扩展的更多相关文章

  1. 第四章 Spring.Net 如何管理您的类___IObjectPostProcessor接口

    官方取名叫 对象后处理器 (object post-processor) , 听起来很高级的样子啊!实际上就是所有实现了这个接口的类,增加了两个方法. Spring.Objects.Factory.C ...

  2. 实现WebMvcConfigurer接口扩展Spring MVC的功能

    前言: 先查看WebMvcConfigurer接口中都定义了哪些内容 public interface WebMvcConfigurer { default void configurePathMat ...

  3. .NET手记-定义类和接口的扩展方法

    对于iOS开发者来说,使用扩展方法是家常便饭.因为有很多的类是有系统框架的定义的,我们不能修改或者不想修改他们的源码,但是我们又想要给他添加一些扩展方法来使用.这时定义扩展方法就是很有用的方式了,正如 ...

  4. Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,输出异常

    Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,出现请求异常时,会进入熔断处理,但是不会抛出异常信息. 经过以下配置,可以抛出异常: 将原有ErrorEncoder ...

  5. 2017.3.31 spring mvc教程(一)核心类与接口

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  6. Spring MVC学习------------核心类与接口

    核心类与接口: 先来了解一下,几个重要的接口与类. 如今不知道他们是干什么的没关系,先混个脸熟,为以后认识他们打个基础. DispatcherServlet   -- 前置控制器 HandlerMap ...

  7. Java基础进阶:多态与接口重点摘要,类和接口,接口特点,接口详解,多态详解,多态中的成员访问特点,多态的好处和弊端,多态的转型,多态存在的问题,附重难点,代码实现源码,课堂笔记,课后扩展及答案

    多态与接口重点摘要 接口特点: 接口用interface修饰 interface 接口名{} 类实现接口用implements表示 class 类名 implements接口名{} 接口不能实例化,可 ...

  8. Spring AOP 介绍与基于接口的实现

    热烈推荐:超多IT资源,尽在798资源网 声明:转载文章,为防止丢失所以做此备份. 本文来自公众号:程序之心 原文地址:https://mp.weixin.qq.com/s/vo94gVyTss0LY ...

  9. Spring @Aspect切面参数传递

    Spring @Aspect切面参数传递: Xml: <?xml version="1.0" encoding="UTF-8"?> <bean ...

随机推荐

  1. fragment 数据传递,传值,通信

    [原][Fragment精深系列4]Fragment与Activity之间的数据交互 2015-5-26阅读389 评论0   以下内容来自于自己的实践和网络资料的整理,希望对你有帮助. 一.Acti ...

  2. Django之路: 基本命令与网址进阶

    一.Django 基本命令 温馨提示:如果你想学习Django,那么就请您从现在开始按照笔记记录一步一步的用手把代码敲出来,千万不要偷懒哦..... 1.创建一个Django project djan ...

  3. IOS之按钮控件--Button全解析及使用 分类: ios技术 2015-01-17 17:09 169人阅读 评论(0) 收藏

    IOS开发中伴随我们始终的 最常用的几个空间之一 -- UIButton 按钮,对于button今天在此做一些浅析,并介绍下主流用法以及常见问题解决办法. 首先是继承问题,UIButton继承于UIC ...

  4. linux下查看cpu物理个数和逻辑个数 - chw1989的专栏 - 博客频道 - CSDN.NET

    body { font-family: 微软雅黑,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLi ...

  5. 转 [ javascript面向对象技术

    以下文章来自iteye,作者是 sdcyst ,个人主页 http://www.iteye.com/topic/288813 类变量/类方法/实例变量/实例方法先补充一下以前写过的方法:在javasc ...

  6. iOS开发概述UIkit动力学,讲述UIKit的Dynamic特性,UIkit动力学是UIkit框架中模拟真实世界的一些特性。

    转发:http://my.oschina.net/u/1378445/blog/335014 iOS UIKit动力学 Dynamics UIAttachmentBehavior 实现iMessage ...

  7. python数据类型之 dict(map)

    字典  一.创建字典  方法①:  >>> dict1 = {}  >>> dict2 = {'name': 'earth', 'port': 80}  >& ...

  8. javascript基础教程学习总结(1)

    摘自javascript基础教程 开始: 1.将脚本放在哪里: 1.1 放在html和<html>之间 范例: <!DOCTYPE html PUBLIC "-//W3C/ ...

  9. Linux-socket 模型理解

    一.socket 一般来说socket有一个别名也叫做套接字. socket起源于Unix,都可以用"打 开open –> 读写write/read –> 关闭close&quo ...

  10. iOS开发——Scheme白名单

    问题:在iOS 9下涉及到平台客户端跳转,系统会自动到项目info.plist下检测是否设置平台Scheme.对于需要配置的平台,如果没有配置,就无法正常跳转平台客户端. 报错:This app is ...