Spring总结 4.AOP之为对象添加新功能
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之为对象添加新功能的更多相关文章
- Spring实战(十)Spring AOP应用——为方法引入新功能、为对象引入新方法
切面最基本的元素是通知和切点,切点用于准确定位应该在什么地方应用切面的通知. 1.Spring借助AspectJ的切点表达式语言来定义Spring切面 在Spring中,要使用AspectJ的切点表达 ...
- vue 数组中嵌套的对象添加新属性--页面更新
vue 数组中嵌套的对象添加新属性--页面更新:https://www.jianshu.com/p/8f0e5bb13735
- React 17 发布候选版本, 没有添加新功能
React 17 发布候选版本, 没有添加新功能 React v17.0 Release Candidate: No New Features https://reactjs.org/blog/202 ...
- phpcms如何给已有的模块添加新功能?
phpcms如何给已有的模块添加新功能? 方法一:直接在模块里的控制器文件中添加功能. 不建议使用此方法,因为一旦phpcms升级,有可能会覆盖模块中的文件, 导致你添加的功能丢失. 方法二:新建一个 ...
- RegisterUserFunc为测试对象添加新方法或重写已有方法
QTP中为了提高扩展性,提供了一个为测试对象添加一个新的自定义方法,或者重写测试对象已有的方法的函数RegisterUserFunc,在此给大家分享一下. RegisterUserFunc:为测试对象 ...
- 一个新人如何学习在大型系统中添加新功能和Debug
文章背景: 今年七月份正式入职,公司主营ERP软件,楼主所在的组主要负责二次开发,使用的语言是Java. 什么叫二次开发呢?ERP软件的客户都是企业.而这些企业之间的情况都有所不同,一套标准版本的企业 ...
- Ecshop:后台添加新功能栏目以及管理权限设置
一.添加菜单项 打开 /admin/includes/inc_menu.php文件(后台框架左边菜单),在最后添加一行如下: $modules['17_other_menu']['sns_list'] ...
- Git -- 新增分支添加新功能
软件开发中,总有无穷无尽的新的功能要不断添加进来. 添加一个新功能时,你肯定不希望因为一些实验性质的代码,把主分支搞乱了,所以,每添加一个新功能,最好新建一个feature分支,在上面开发,完成后,合 ...
- vue data对象添加新属性触发视图
<template> <div class="wrap open"> <a>{{test01.name}}</a> <a> ...
随机推荐
- pickle 继承
1.什么是方法,什么是函数 class Foo: def chi(self): print("我是吃") @staticmethod def static_method(): pa ...
- jQuery插件初级练习1
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- 中美会计准则差异比较(PRC GAAP VS US GAAP)
http://bbs.chinaacc.com/forum-2-43/topic-2023118.html 一.中美会计准则的实质性差别 rule based vs principle base ...
- XML Publisher 并发程序由于"输出提交处理程序提交失败
http://www.cnblogs.com/benio/archive/2012/03/30/2424900.html xmlp 报表运行完成后,状态为warning,其原因大概有以下3类:1.&q ...
- JQuery实用技巧
1.关于页面元素的引用通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用dom ...
- 【VB.NET】通过 IPIP.NET 数据库来查询IP地址
上一次介绍了利用纯真数据库查询IP地址详细信息的方法.然而纯真数据库是由网友反馈所提供的,很多数据描述并不准确,所以我上网找了一些其他的IP数据库,最后就找到了 ipip.net 这个网站所提供的IP ...
- Linux-切换启动方式
Linx 默认的启动方式可以用图形界面也可以用命令行状态,命令行状态的启动相对来说运行速度更快,而且资源的消耗也更小,这个可以在Linux启动的过程中修改,也可直接修改配置文件来进行设置默认的启动方式 ...
- wpf根据X与Y轴获取内部值
效果图: 环境 Visual Studio ,.Net Framework 4.0 SDK 支持Windows XP,Windows Win7.. 实现方法: 创建ListBox设置ListBox.I ...
- 通过EntityFramework操作sqlite(DbFirst)
记录一下通过 EntityFramework6 来操作sqlite过程 环境: visual studio 2017 .net 4.5 Console Application(项目类型) sqlite ...
- MVC使用TempData将返回的string传到另一个控制器页面中显示!
我需要把数据库中查询出的数据,传递到另一个控制器的页面去显示. https://blog.csdn.net/ma_jiang/article/details/8164212 看了上面这篇文章,反应过来 ...