AOP_通过切面引入新功能(通过切面为方法添加实现接口)
2016-12-11 15:54
309 查看
在有些情况下某个类并不能显式的添加实现类,比如我们只有class文件,此时我们需要为这个类添加一个接口,就可以使用aop的切面来配置。
一、XML配置
1.目标类
package cn.xt.aop;
public class ExtraClass {
}
2.需要实现的接口
package cn.xt.aop;
public interface INewProduct {
public void extra();
}
3.接口的某个实现类
package cn.xt.aop;
public class NewProductImpl implements INewProduct{
@Override
public void extra() {
System.out.println("这是为通知类扩展的方法.....");
}
}
4.xml配置
<!-- 需要扩展的类 -->
<bean class="cn.xt.aop.ExtraClass" id="extraClass"></bean>
<!-- 扩展需要的实现类 -->
<bean class="cn.xt.aop.NewProductImpl" id="newProductImpl"></bean>
<aop:config>
<aop:aspect><!-- 通过切面为类引入新功能(添加默认实现的接口和实现类) -->
<aop:declare-parents
types-matching="cn.xt.aop.ExtraClass"
implement-interface="cn.xt.aop.INewProduct"
delegate-ref="newProductImpl" />
</aop:aspect>
</aop:config>
delegate-ref属性代表引用spring容器中的bean来作为默认实现类,另外可以用default-impl="实现类的全限定名"实现一样的效果
5.测试
//通过切面配置以后,ExtraClass并没有显式实现INewProduct接口,依然可以调用接口方法
INewProduct ec = (INewProduct) ctx.getBean("extraClass",ExtraClass.class);
ec.extra();
二、注解配置
1.声明一个切面
@Aspect
public class ExtraAspect {
@DeclareParents(
value="cn.xt.aop.annotation.ExtraClass"
,
defaultImpl=NewProductImpl.class
)
public static INewProduct newProject;
}
2.将切面声明为spring的一个bean
<bean class="cn.xt.aop.annotation.ExtraAspect" />
3.将扩展的实体声明为spring的一个bean
<!-- 需要扩展的类 -->
<bean class="cn.xt.aop.annotation.ExtraClass" id="extraClass"></bean>
4.测试
INewProduct ec = (INewProduct) ctx.getBean("extraClass",ExtraClass.class);
ec.extra();
一、XML配置
1.目标类
package cn.xt.aop;
public class ExtraClass {
}
2.需要实现的接口
package cn.xt.aop;
public interface INewProduct {
public void extra();
}
3.接口的某个实现类
package cn.xt.aop;
public class NewProductImpl implements INewProduct{
@Override
public void extra() {
System.out.println("这是为通知类扩展的方法.....");
}
}
4.xml配置
<!-- 需要扩展的类 -->
<bean class="cn.xt.aop.ExtraClass" id="extraClass"></bean>
<!-- 扩展需要的实现类 -->
<bean class="cn.xt.aop.NewProductImpl" id="newProductImpl"></bean>
<aop:config>
<aop:aspect><!-- 通过切面为类引入新功能(添加默认实现的接口和实现类) -->
<aop:declare-parents
types-matching="cn.xt.aop.ExtraClass"
implement-interface="cn.xt.aop.INewProduct"
delegate-ref="newProductImpl" />
</aop:aspect>
</aop:config>
delegate-ref属性代表引用spring容器中的bean来作为默认实现类,另外可以用default-impl="实现类的全限定名"实现一样的效果
5.测试
//通过切面配置以后,ExtraClass并没有显式实现INewProduct接口,依然可以调用接口方法
INewProduct ec = (INewProduct) ctx.getBean("extraClass",ExtraClass.class);
ec.extra();
二、注解配置
1.声明一个切面
@Aspect
public class ExtraAspect {
@DeclareParents(
value="cn.xt.aop.annotation.ExtraClass"
,
defaultImpl=NewProductImpl.class
)
public static INewProduct newProject;
}
2.将切面声明为spring的一个bean
<bean class="cn.xt.aop.annotation.ExtraAspect" />
3.将扩展的实体声明为spring的一个bean
<!-- 需要扩展的类 -->
<bean class="cn.xt.aop.annotation.ExtraClass" id="extraClass"></bean>
4.测试
INewProduct ec = (INewProduct) ctx.getBean("extraClass",ExtraClass.class);
ec.extra();
相关文章推荐
- asp.net通过实现IHttpHandler接口方法给图片添加水印图片(动态添加,不破坏原图片,但是耗资源)
- 通过切面为特定类引入新功能 aop:declare-parents
- 通过实现Cloneable接口和覆盖clone()方法实现深度克隆,以及如何通过ByteArrayIOStream实现克隆
- C# 通过接口IEnumerator让自己编写的对象/类,实现foreach遍历方法
- mybatis通过接口实现增删改查方法
- Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- Spring 通过注解方式实现AOP切面编程
- Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- 地磅称量系统之(53)在封装对象的类库中实现包括IDataErrorInfo接口提供的所有方法和并且扩展对异常具有添加和删除功能的基本业务对象基类
- Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- 使用Spring的AOP实现接口方法执行时间记录
- 实现多接口 1手动添加接口。2接口升级:IMathe接口添加新功能升级到IMathe2
- 通过接口引用访问实现比正常的方法调用开销大
- 启动线程的五种方式方法(通过继承Thread类或实现Runnable接口)
- 在javascript中实现OO,通过prototype为类型添加方法,好像继承
- 用接口实现通过改外部文件实现判断调用接口内的方法
- 解决 通过继承QAbstractTableModel并实现headerData方法给QTableView添加标题时,标题不显示 的问题
- Spring 【Aspectj】【注解】引入通知 ------ 为【目标对象】添加,【新接口】及【实现】
- 通过接口实现压缩文件、目录,修正遍历方式,未添加排除文件操作
- NopCommerce添加新功能“类型属性”【添加服务接口及服务实现】