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. dedecms 的这个dede:arclist里怎么调用全局变量?

    将{dede:global.cfg_templets_skin/} 写为 [field:global.cfg_templets_skin/] 即可.

  2. js判断各个浏览器

    其实,实际写js的过程中,用功能判断”鸭shi辨形“的方法就可以了,不过,很多地方考试还是会问.所以总结一下,大部分都是分析navigator来实现的. var ua=navigator.userAg ...

  3. unbtun python tab补全

    在使用python的时候有时候总是忘记很多代码,这个是作为程序袁最头疼的事情,本人也是刚刚接触python,这几天也是用到这块,所以记录下来,已被需要时能够找到. 我的系统是:  w@w:~$ una ...

  4. php使用curl设置超时的重要性

    原文:http://phpquan.com/lamp/php/php-curl-timeout/ 网站登录不了,原因是没有可用的 PHP 子进程来响应新的请求了.这可能是是由于PHP-curl  没有 ...

  5. php 读取二进制文件

    $file_pointer = fopen($file, "r"); $file_read = fread($file_pointer, filesize($file)); //$ ...

  6. 结合Pnotify插件--app-jquery-notify.js

    $.NOTIFY = { showSuccess : function (title, text, context) { var opt = { title : title, text : text, ...

  7. 关联规则算法之FP growth算法

    FP树构造 FP Growth算法利用了巧妙的数据结构,大大降低了Aproir挖掘算法的代价,他不需要不断得生成候选项目队列和不断得扫描整个数据库进行比对.为了达到这样的效果,它采用了一种简洁的数据结 ...

  8. MySQL create table 语法

    MySQL中create table语句的基本语法是: CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name     [(create_definitio ...

  9. (中等) POJ 2482 Stars in Your Window,静态二叉树。

    Description Here comes the problem: Assume the sky is a flat plane. All the stars lie on it with a l ...

  10. OPENCV条形码检测与识别

    条形码是当前超市和部分工厂使用比较普遍的物品,产品标识技术,使用摄像头检测一张图片的条形码包含有两个步骤,第一是定位条形码的位置,定位之后剪切出条形码,并且识别出条形码对应的字符串,然后就可以调用网络 ...