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> ...
随机推荐
- (数学)Knight's Trip -- hdu -- 3766
http://acm.hdu.edu.cn/showproblem.php?pid=3766 Knight's Trip Time Limit: 2000/1000 MS (Java/Others) ...
- POJ2229--Sumsets(动态规划)
Farmer John commanded his cows to search for different sets of numbers that sum to a given number. T ...
- java基础-day17
第06天 集合 今日内容介绍 u 集合&迭代器 u 增强for & 泛型 u 常见数据结构 u List子体系 第1章 集合&迭代器 1.1 集合体系结构 1.1 ...
- hdu 4964 恶心模拟
http://acm.hdu.edu.cn/showproblem.php?pid=4964 给定语句,按照语法翻译html并输出. 就是恶心的模拟,递归搞就行了 处理id和class时,在一个'&g ...
- Git 安装 windows && linux
一.安装: windows下安装Git: 1.下载Git:https://git-scm.com/download/win 2.安装Git:默认安装,一直回车 Linux下安装Git: yum安装: ...
- 广搜 poj3278 poj1426 poj3126
Catch That Cow Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Ja ...
- bash编程-Shell基础
1. Shell脚本执行方式 直接运行,需要在脚本文件头部指定解释器,如#!/bin/bash ./myshell.sh 运行时指定shell解释器 bash myshell.sh 2. Shell命 ...
- CentOS 7.0 Firewall防火墙配置
启动停止 获取firewall状态 systemctl status firewalld.service firewall-cmd --state 开启停止防火墙 开机启动:systemctl ena ...
- 第21件事 资源支持离不开RACI表
十步法的第九步寻求资源支持.资源主要包括人力资源.物力资源和财力资源.人力资源,即需要多少人:物力资源,即需要多少软硬件设备:财力资源,即需要多少预算.根据产品或项目目标,资源估算时要考虑需要什么样的 ...
- Python 学习第三部分函数——第一章函数基础
函数是python 为了代码最大程度的重用和最小代码冗余而提供的最基本的程序结构.使用它我们可以将复杂的系统分解为可管理的部件. 函数相关语句 def... 创建一个对象并将其赋值给 ...