转发:https://www.iteye.com/blog/elim-2395410

6 @DeclareParents介绍

@DeclareParents注解也是Aspectj提供的,在使用基于Aspectj注解的Spring Aop时,我们可以在切面中通过@DeclareParents指定满足指定表达式的类将自动实现某些接口。这个只是在运行时会将生成的代理类实现指定的接口。有接口就会有实现,对应的实现类也需要我们在@DeclareParents声明自动实现的接口时声明。现假设我们有一个接口叫CommonParent,其实现类叫CommonParentImpl,代码如下。

public interface CommonParent {

	public void doSomething();

}
public class CommonParentImpl implements CommonParent { public void doSomething() {
System.out.println("-----------do something------------");
} }

然后我们希望我们的所有的Service实现类都可以在运行时自动实现CommonParent接口,即所有的Service实现类在运行时都可以被当做CommonParent来使用。那我们可以定义如下这样一个切面类和对应的Advice。

@Component
@Aspect
public class DeclareParentsAspect { @DeclareParents(value="com.elim.spring.aop.service..*", defaultImpl=CommonParentImpl.class)
private CommonParent commonParent; @Before("bean(userService) && this(commonParent)")
public void beforeUserService(CommonParent commonParent) {
commonParent.doSomething();
} }

如上,我们先在切面类中声明了一个CommonParent类型的属性,然后在上面使用了@DeclareParents注解表示我们需要把CommonParent声明为某些指定类型的父接口,然后通过@DeclareParents的value属性指定需要作用的类的形式,其语法和Pointcut表达式类似。通过defaultImpl属性指定默认CommonParent接口的实现类是CommonParentImpl。然后我们声明了一个before类型的Advice,在其中直接把我们的bean当做CommonParent类型的对象使用。

整个过程就是这样的,非常简单。但是笔者暂时还没有发现这个在实际应用中可以应用于哪些场景,欢迎交流。

参考文档 1、官方文档

(注:本文是基于Spring4.1.0所写,写于2017年1月22日星期日)

Spring Aop(六)——@DeclareParents介绍的更多相关文章

  1. Spring入门篇——第6章 Spring AOP的API介绍

    第6章 Spring AOP的API介绍 主要介绍Spring AOP中常用的API. 6-1 Spring AOP API的Pointcut.advice概念及应用 映射方法是sa开头的所有方法 如 ...

  2. spring aop做什么介绍

    1.AOP(Aspect Orient Programming),称为面向切面编程,它作为面向对象(OOP)的一种补充,用于处理系统中分布于各个模板的横切关注点,比如事务管理.日志.缓存等.AOP实现 ...

  3. spring AOP(切面) 表达式介绍

    在 spring AOP(切面) 例子基础上对表达式进行介绍 1.添加接口删除方法 2.接口实现类 UserDaoServer 添加实现接口删除方法 3.测试类调用delUser方法 4. 输出结果截 ...

  4. Spring Aop重要概念介绍及应用实例结合分析

    转自:http://bbs.csdn.net/topics/390811099 此前对于AOP的使用仅限于声明式事务,除此之外在实际开发中也没有遇到过与之相关的问题.最近项目中遇到了以下几点需求,仔细 ...

  5. spring---aop(7)---Spring AOP中expose-proxy介绍

    写在前面 expose-proxy.为是否暴露当前代理对象为ThreadLocal模式. SpringAOP对于最外层的函数只拦截public方法,不拦截protected和private方法(后续讲 ...

  6. spring---aop(6)---Spring AOP中ProxyFactoryBean介绍

    写在前面 这篇文章里面就要说说Spring自己的AOP,搞清楚哪种方式是Spring自己实现的AOP,哪种方式是Spring引入aspectj的AOP. 简单例子 Spring自己的AOP实现在于Pr ...

  7. 使用Redisson实现分布式锁,Spring AOP简化之

    源码 Redisson概述 Redisson是一个在Redis的基础上实现的Java驻内存数据网格(In-Memory Data Grid).它不仅提供了一系列的分布式的Java常用对象,还提供了许多 ...

  8. Spring AOP 之二:Pointcut注解表达式

    简介 在Spring AOP概述中我们重点注意的是AOP的整体流程和Advice,简化了一些其他的东西,其中就有一些对灵活应用Spring AOP很重要的知识点,例如Pointcut表达式,下面就介绍 ...

  9. Spring 4 官方文档学习(六)核心技术之Spring AOP

    目录 1.介绍 1.1.AOP概念 1.2.Spring AOP 能力 和 目标 1.2.1.简介 1.2.2.@AspectJ 支持 1.2.3.声明一个aspect 例子 1.2.4.声明advi ...

随机推荐

  1. python logger日志配置

    self.logger = logging.getLogger(logName) # 创建logger实例 time = datetime.datetime.now() logFilePath = o ...

  2. 显示名为“xxx.XmlSerializers”的程序集未能加载到 ID 为 1 的 AppDomain 的“LoadFrom”绑定上下文中。

    VS调试程序运行中提示“显示名为“xxx.XmlSerializers”的程序集未能加载到 ID 为 1 的 AppDomain 的“LoadFrom”绑定上下文中.错误的原因为: System.IO ...

  3. mysql基础篇--库的管理

    库的创建 create database [if not exists] 库名; 库的修改 alter database 库名 character set 字符集; #更改库的字符集 库的删除 dro ...

  4. ubuntu 16.04 TLS 安装VNC

    安装 #autocuftsel 用于vnc两段的复制粘贴 sudo apt-get install autocutsel 安装 vncserversudo apt-get install --no-i ...

  5. 如何使用python异常---runtimeError方法

    RuntimeError def ilove(name): if name=='陈培昌': print('i love {0}'.format(name)) elif name == '程劲': pr ...

  6. 后缀自动机再复习 + [USACO17DEC] Standing Out from the Herd

    here:https://oi-wiki.org/string/sam/ 下面转自 KesdiaelKen的雷蒻论坛 来个广义后缀自动机模板题 [USACO17DEC]Standing Out fro ...

  7. .Net Core:Middleware自定义中间件

    新建standard类库项目,添加引用包 Microsoft.AspNetCore 1.扩展IApplicationBuilder using Microsoft.AspNetCore.Builder ...

  8. 002转载----C# 基于OneNet 的物联网数据通信

    作者:lnwin521 来源:CSDN 原文:https://blog.csdn.net/lnwin521/article/details/84549606 (遇到404情况请复制粘贴后再打开)版权声 ...

  9. form表单 一个input时 回车自动提交

    问题描述 form表单中,如果当前表单只有一个input输入框时,单击回车会自动提交当前表单. 解决方案 在当前form表单中添加一个隐藏的input, <input style="d ...

  10. Oracle 体系结构图

    Oracle体系结构图 详细图