转发: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. 分享一波目前写的最强的autohotkey 插件

    支持各种软件快速切换,补全括号,代码等!!!!!!!! ;这种全局定义要写在所有代码的前面才能让所有代码起作用. SetCapsLockState , AlwaysOff SetNumlockStat ...

  2. Linux使用帮助详解

    主要内容:                    whatis                    command --help                     man and info   ...

  3. 02-命令篇——基础命令&常用命令

    基础命令 docker 启动与停止 启动docker systemctl start docker 停止docker systemctl stop docker 重启docker systemctl ...

  4. mali --mobile platform GPU arch about vertex pipeline

    顶点这边 我看powerVR也就是说苹果也是如此 还有mali 目前看这俩平台的顶点数据都有这样一步优化 一个render pass的所有顶点shade的时候 先算pos 用这个结果拿最上一层顶点数据 ...

  5. C++ extern关键字

    [参考] 关于C++中extern的简单笔记

  6. Gradle 发布 Jar 到 Archiva 时提示不能 Overwriting released artifacts is not allowed

    系统提示错误信息: Received status code 409 from server: Overwriting released artifacts is not allowed. 这是在 A ...

  7. [Luogu] 网络

    https://www.luogu.org/problemnew/show/P3250 树链剖分 + 线段树 + 优先队列 要求未被影响的请求中最大的 所以每次将每条路径在整棵树上的补集的每个节点的优 ...

  8. UVA1674 闪电的能量 树剖

    UVA1674 闪电的能量 树剖 题面 水.树剖模板 #include <cstdio> #include <cstring> #include <algorithm&g ...

  9. 18.4.1 考试解题报告 P71

    题目:https://files.cnblogs.com/files/lovewhy/problem.pdf 偷偷摘来dalao题面. P71竞赛时间:???? 年?? 月?? 日??:??-??:? ...

  10. The 2019 ICPC China Nanchang National Invitational and International Silk-Road Programming Contest B、H

    比赛链接https://www.jisuanke.com/contest/3098?view=challenges B题 拉格朗日插值 题意  T组输入.一个n次多项式 f(x) ,每项的系数不知道, ...