Spring除了提供增强原有功能的方法外,还提供了为一个对象引入新功能的方法。如下:

package cn.powerfully.service;

public interface IService {
public void doSomething(int num);
}
package cn.powerfully.service;

public class ServiceImpl implements IService {

    @Override
public void doSomething(int num) {
System.out.println("doSomething --" + num);
} }

  现在为该类的实例化对象引入新方法f()。首先先定义一个接口表示要引入该接口的方法:

package cn.powerfully.service;

public interface IExtraService {
public void f();
}

  既然要有引入新功能,除了接口那肯定还要实现类:

package cn.powerfully.service;

public class ExtraServiceImpl implements IExtraService {

    @Override
public void f() {
System.out.println("add");
} }

  接着使用<aop:declare-parents>标签进行配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
"> <bean id="aspect" class="cn.powerfully.aspect.Aspect" /> <bean id="userService" class="cn.powerfully.service.ServiceImpl" /> <aop:config>
<aop:aspect ref="aspect">
<aop:declare-parents types-matching="cn.powerfully.service.IService+"
implement-interface="cn.powerfully.service.IExtraService"
default-impl="cn.powerfully.service.ExtraServiceImpl" />
</aop:aspect>
</aop:config> </beans>

  其中,types-matching表示需要引入新功能的目标类,IService+表示了实现了IService接口的类。implement-interface表示新增的接口,default-impl表示其接口默认实现类。

  这样,就完成所有配置。对它进行测试:

package cn.powerfully.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.powerfully.service.IExtraService;
import cn.powerfully.service.IService; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:beans.xml")
public class Demo { @Resource
private IService service; @Test
public void test1() throws Exception {
service.doSomething(1);
IExtraService s = (IExtraService)service;
s.f();
} }

Spring总结 4.AOP之为对象添加新功能的更多相关文章

  1. Spring实战(十)Spring AOP应用——为方法引入新功能、为对象引入新方法

    切面最基本的元素是通知和切点,切点用于准确定位应该在什么地方应用切面的通知. 1.Spring借助AspectJ的切点表达式语言来定义Spring切面 在Spring中,要使用AspectJ的切点表达 ...

  2. vue 数组中嵌套的对象添加新属性--页面更新

    vue 数组中嵌套的对象添加新属性--页面更新:https://www.jianshu.com/p/8f0e5bb13735

  3. React 17 发布候选版本, 没有添加新功能

    React 17 发布候选版本, 没有添加新功能 React v17.0 Release Candidate: No New Features https://reactjs.org/blog/202 ...

  4. phpcms如何给已有的模块添加新功能?

    phpcms如何给已有的模块添加新功能? 方法一:直接在模块里的控制器文件中添加功能. 不建议使用此方法,因为一旦phpcms升级,有可能会覆盖模块中的文件, 导致你添加的功能丢失. 方法二:新建一个 ...

  5. RegisterUserFunc为测试对象添加新方法或重写已有方法

    QTP中为了提高扩展性,提供了一个为测试对象添加一个新的自定义方法,或者重写测试对象已有的方法的函数RegisterUserFunc,在此给大家分享一下. RegisterUserFunc:为测试对象 ...

  6. 一个新人如何学习在大型系统中添加新功能和Debug

    文章背景: 今年七月份正式入职,公司主营ERP软件,楼主所在的组主要负责二次开发,使用的语言是Java. 什么叫二次开发呢?ERP软件的客户都是企业.而这些企业之间的情况都有所不同,一套标准版本的企业 ...

  7. Ecshop:后台添加新功能栏目以及管理权限设置

    一.添加菜单项 打开 /admin/includes/inc_menu.php文件(后台框架左边菜单),在最后添加一行如下: $modules['17_other_menu']['sns_list'] ...

  8. Git -- 新增分支添加新功能

    软件开发中,总有无穷无尽的新的功能要不断添加进来. 添加一个新功能时,你肯定不希望因为一些实验性质的代码,把主分支搞乱了,所以,每添加一个新功能,最好新建一个feature分支,在上面开发,完成后,合 ...

  9. vue data对象添加新属性触发视图

    <template> <div class="wrap open"> <a>{{test01.name}}</a> <a> ...

随机推荐

  1. Leetcode--1. Two Sum(easy)

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  2. ScheduledExecutorService的使用

    http://407827531.iteye.com/blog/1329597 ScheduledExecutorService接口 在ExecutorService的基础上,ScheduledExe ...

  3. Linqpad工具

    下载地址: http://www.linqpad.net/ 可以通过连接数据库,直接测试LINQ语句,并可以转化为Lamda .SQL语句. 非常好用的小工具,记录一下.

  4. IOS绘图详解

    http://blog.163.com/wkyuyang_001/blog/static/10802122820133190545227/

  5. Maven中的配置文件

    <span style="padding:0px; margin:0px"><project xmlns="http://maven.apache.or ...

  6. 分形之康托(Cantor)三分集

    1883年,德国数学家康托(G.Cantor)提出了如今广为人知的三分康托集,或称康托尔集.三分康托集是很容易构造的,然而,它却显示出许多最典型的分形特征.它是从单位区间出发,再由这个区间不断地去掉部 ...

  7. RGB-D 室内导航 paper

    摘要: 最近打算使用Kinect实现机器人的室内导航,收集了近年来的一些比较好的文章.<基于Kinect系统的场景建模与机器人自主导航>.<Mobile Robots Navigat ...

  8. Spring Security Hello World Example--reference

    In this tutorial, we will see how we can use Spring security to protect specific resources. This Hel ...

  9. 一不小心发现了个Asp.Net Bug

    1. Ver是页面定义的变量 2. asp.net 页面定义为  <link href="/company/them/page.css?v=<%=Ver%>" r ...

  10. 第二节:创建模型,使用Code First,配置映射关系

    这一节,实现模型的创建,配置映射关系 使用Code First数据迁移. 创建模型 一,首先创建几个接口:实体接口,聚合根接口,值对象接口 1,实体接口: 2,聚合根接口: 3,值对象接口: 二,模型 ...