Spring @Aspect进行类的接口扩展
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进行类的接口扩展的更多相关文章
- 第四章 Spring.Net 如何管理您的类___IObjectPostProcessor接口
官方取名叫 对象后处理器 (object post-processor) , 听起来很高级的样子啊!实际上就是所有实现了这个接口的类,增加了两个方法. Spring.Objects.Factory.C ...
- 实现WebMvcConfigurer接口扩展Spring MVC的功能
前言: 先查看WebMvcConfigurer接口中都定义了哪些内容 public interface WebMvcConfigurer { default void configurePathMat ...
- .NET手记-定义类和接口的扩展方法
对于iOS开发者来说,使用扩展方法是家常便饭.因为有很多的类是有系统框架的定义的,我们不能修改或者不想修改他们的源码,但是我们又想要给他添加一些扩展方法来使用.这时定义扩展方法就是很有用的方式了,正如 ...
- Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,输出异常
Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,出现请求异常时,会进入熔断处理,但是不会抛出异常信息. 经过以下配置,可以抛出异常: 将原有ErrorEncoder ...
- 2017.3.31 spring mvc教程(一)核心类与接口
学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...
- Spring MVC学习------------核心类与接口
核心类与接口: 先来了解一下,几个重要的接口与类. 如今不知道他们是干什么的没关系,先混个脸熟,为以后认识他们打个基础. DispatcherServlet -- 前置控制器 HandlerMap ...
- Java基础进阶:多态与接口重点摘要,类和接口,接口特点,接口详解,多态详解,多态中的成员访问特点,多态的好处和弊端,多态的转型,多态存在的问题,附重难点,代码实现源码,课堂笔记,课后扩展及答案
多态与接口重点摘要 接口特点: 接口用interface修饰 interface 接口名{} 类实现接口用implements表示 class 类名 implements接口名{} 接口不能实例化,可 ...
- Spring AOP 介绍与基于接口的实现
热烈推荐:超多IT资源,尽在798资源网 声明:转载文章,为防止丢失所以做此备份. 本文来自公众号:程序之心 原文地址:https://mp.weixin.qq.com/s/vo94gVyTss0LY ...
- Spring @Aspect切面参数传递
Spring @Aspect切面参数传递: Xml: <?xml version="1.0" encoding="UTF-8"?> <bean ...
随机推荐
- Highcharts一些属性
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- mac home/end/pageup/pageDown
home:按fn+左键 end:按fn+右键 pageup:按fn+上键 pagedown:按fn+下键
- 操作IFRAME及元素
内容里有两个ifame <iframe id="leftiframe"...</iframe> <iframe id="mainiframe..& ...
- CSS的继承性和层叠性
1.继承性 比不是所有的属性都能继承,只有一些文本的属性才能被继承.比如:color,text-开头的,line-开头的,font-开头的.这几种属性才能被继承. 其余的关于盒子,定位,布局的都不能被 ...
- NIPS 2016论文:英特尔中国研究院在神经网络压缩算法上的最新成果
NIPS 2016论文:英特尔中国研究院在神经网络压缩算法上的最新成果 http://www.leiphone.com/news/201609/OzDFhW8CX4YWt369.html 英特尔中国研 ...
- hbuilder 手机app开发系列(一)
最佳答案好水啊,实在看不过眼,首先apicloud是一个框架,hbuidler是ide工具,两者没什么可比性.我来推荐一个国外免费开源的项目吧,Ionic framework,我之所以推荐它是因为它支 ...
- Docker - 容器编排工具 compose 之安装
准备 首先,在使用和安装 docker compose之前,我们应该确保我们已经安装了 docker engine. 安装 官网上面有好多种安装方式,由于我们现在是在使用Docker, 个人感觉应该以 ...
- opencv图像特征检测之斑点检测
前面说过,图像特征点检测包括角点和斑点,今天来说说斑点,斑点是指二维图像中和周围颜色有颜色差异和灰度差异的区域,因为斑点代表的是一个区域,所以其相对于单纯的角点,具有更好的稳定性和更好的抗干扰能力. ...
- iOS Run loop使用实例
http://blog.csdn.net/libaineu2004/article/details/45364737 一.Runloop简介: Run loops 是线程相关的的基础框架的一部分.一个 ...
- 【转】国外程序员整理的 C++ 资源大全
内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包括了STL容器,算法和函数等. C++ Standard Library:是一系列类 ...