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. Kafka auto.offset.reset

    要从头消费kafka的数据,可以通过以下参数: Kafka auto.offset.reset = earliest

  2. DXP中插入LOGO字体方法(2)

    利用字体制作软件font creator program 4.1 1.文件-->新建 2.右键---->属性 3.去掉黑框和黑底,删除即可! 4.选 工具--->导入图像,载入字体图 ...

  3. ue4开发入门教程

    ue4是一套开源跨平台的游戏引擎,游戏画质3A水准,具有强大的材质编辑器,各种插件齐全.想要学习ue4的,可以参考这篇文章作为入门. 学习这篇文章,建议具有一定的C++基础,对游戏有一定了解. 1.首 ...

  4. MQ的demo

    public class WorkTest {  @Test  public void send() throws Exception{    //获取连接    Connection conn = ...

  5. Lerning Entity Framework 6 ------ Defining the Database Structure

    There are three ways to define the database structure by Entity Framework API. They are: Attributes ...

  6. Linux系统软件包的管理(4)

    虽然使用源码编译安装可以具有提高速度个性化的定制等优点,但对于 Linux发行商来说,则不容易管理软件包,毕竟不是每个人都会进行源码编译的,如果能够将软件预先在相同的硬体与系统上面编译好在发布的话,不 ...

  7. 分组,命名分组,url的命名和反向解析

    1.位置分组 匹配到参数,按照位置参数的方式传递给视图函数 视图函数需要定义形参接收变量 1.写在url里面的: # 删除 url(r'^del_class/(\d+)',views.del_clas ...

  8. es6新增Math方法

    Math.trunc()  用于去除一个数的小数部分,只返回整数部分 Math.trunc(4.1) // 4 Math.trunc(4.9) // 4 Math.trunc(-4.1) // -4 ...

  9. Spring中使用StandardServletMultipartResolver进行文件上传

    从Spring3.1开始,Spring提供了两个MultipartResolver的实现用于处理multipart请求,分别是:CommonsMultipartResolver和StandardSer ...

  10. Selenium3 + Python3自动化测试系列四——鼠标事件和键盘事件

    一.鼠标事件 在 WebDriver 中, 将这些关于鼠标操作的方法封装在 ActionChains 类提供. ActionChains 类提供了鼠标操作的常用方法. ActionChains 类的成 ...