Spring AOP之Introduction(@DeclareParents)简介
Spring的文档上对Introduction这个概念和相关的注解@DeclareParents作了如下介绍:
Introductions (known as inter-type declarations in AspectJ) enable an aspect to declare that advised objects implement a given interface, and to provide an implementation of that interface on behalf of those objects.
An introduction is made using the @DeclareParents annotation. This annotation is used to declare that matching types have a new parent (hence the name).
在这段介绍之后还给出了一个例子,对于初学者要理解这段话以及后面的例子还是蛮困难的,因此下面用一个简单的例子告诉大家什么是Introduction以及如何使用@DeclareParents注解。
对于Introduction这个词,个人认为理解成引入是最合适的,其目标是对于一个已有的类引入新的接口(有人可能会问:有什么用呢?简单的说,你可以把当前对象转型成另一个对象,那么很显然,你就可以调用另一个对象的方法了),看一个例子就全明白了。
假设已经有一个UserService类提供了保存User对象的服务,但是现在想增加对User进行验证的功能,只对通过验证的User提供保存服务,在不修改UserService类代码的前提下就可以通过Introduction来解决。
首先定义一个Verifier接口,里面定义了进行验证的方法validate(),如下所示
public interface Verifier {
public boolean validate(User user);
}
接下来给出该接口的一个实现类BasicVerifier,如下所示
public class BasicVerifier implements Verifier {
@Override
public boolean validate(User user) {
if(user.getUsername().equals("jack") && user.getPassword().equals("1234")) {
return true;
}
return false;
}
}
如何才能为UserService类增加验证User的功能呢,如下所示定义Aspect:
@Aspect
@Component
public class MyAspect {
@DeclareParents(value="com.tsinghuait.services.UserService",
defaultImpl=com.tsinghuait.aop.BasicVerifier.class)
public Verifier verifer;
}
接下来就可以将UserService对象转型为Verifier对象并对用户进行验证了,如下所示:
class Test {
public static void main(String[] args) {
User user1 = new User();
user1.setUsername("abc");
user1.setPassword("def");
ApplicationContext factory = new ClassPathXmlApplicationContext("config.xml");
Service s = (Service) factory.getBean("service");
Verifier v = (Verifier) s;
if(v.validate(user1) {
System.out.println("验证成功");
s.serve(user1);
}
}
}
这样,上面代码中的user1是不会被服务的,当然是因为没有通过验证啦!
这样一说,是不是大概明白什么是Introduction了呢,其实@DeclareParents用起来也很简单吧!
Spring AOP之Introduction(@DeclareParents)简介的更多相关文章
- Spring AOP之Introduction(@DeclareParents)简介(转)
Spring的文档上对Introduction这个概念和相关的注解@DeclareParents作了如下介绍: Introductions (known as inter-type declarati ...
- 循序渐进之Spring AOP(4) - Introduction
前面描述的几种增强(Advice)都是在目标方法范围内织入,而引介(Introduction)不同,直接在类级别上添加目标未实现的接口方法. 在spring中可以通过扩展DelegatingIntro ...
- Spring学习十五----------Spring AOP API的Pointcut、advice及 ProxyFactoryBean相关内容
© 版权声明:本文为博主原创文章,转载请注明出处 实例: 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4. ...
- Spring AOP简介与底层实现机制——动态代理
AOP简介 AOP (Aspect Oriented Programing) 称为:面向切面编程,它是一种编程思想.AOP 是 OOP(面向对象编程 Object Oriented Programmi ...
- Spring Aop(一)——Aop简介
转发地址:https://www.iteye.com/blog/elim-2394629 1 Aop简介 AOP的全称是Aspect Oriented Programming,翻译成中文是面向切面编程 ...
- Spring AOP简介
AOP简述 AOP的概念早在上个世纪九十年代初就已经出现了,当时的研究人员通过对面向对象思想局限性的分析研究出了一种新的编程思想来帮助开发者减少代码重复提高开发效率,那就是AOP,Aspect-Ori ...
- Spring AOP 简介
Spring AOP 简介 如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中切面编程被广泛使用. AOP 即 Aspect Orien ...
- Spring(十七):Spring AOP(一):简介
背景: 需求: 给一个计算器计算函数执行前后添加日志. 实现: 1)直接在函数中修改代码: IArithmeticCalculator.java接口类 package com.dx.spring.be ...
- Spring AOP 简介(三)
Spring AOP 简介 如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中切面编程被广泛使用. AOP 即 Aspect Orien ...
随机推荐
- 大数据开发实战:Hadoop数据仓库开发实战
1.Hadoop数据仓库架构设计 如上图. ODS(Operation Data Store)层:ODS层通常也被称为准备区(Staging area),它们是后续数据仓库层(即基于Kimball维度 ...
- hdu 3665Seaside(简单floyd)
Seaside Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- WebApi&MVC对比
使用上区分,mvc主要用于建站,web api主要用于构建http服务,当然你非要用mvc来构建Uri式的Api也行,不过显然是没有这个必要的,一个不恰当的比喻就像是你也可以玩破解版的单机游戏,也可以 ...
- Linux系统中查找、删除重复文件,释放磁盘空间。
在Linux系操作系统中查找并删除重复文件的方法的确有很多,不过这里介绍的是一款非常简单实用的软件FSlint.FSlint是一个重复文件查找工具,可以使用它来清除不必要的重复文件,笔者经常使用它来释 ...
- Cognos11中通过URL访问report的设置
1:以往的cognos版本中在报表的属性中可以找到 url的属性,稍加修改就可以通过URL进行访问了 2:Cognos11中找了半天也没有报表URL这个属性,但是IBM官方也给出了解决方案 Answe ...
- KVM 虚拟化技术
1.1 前言 1.1.1 什么是虚拟化? 在计算机技术中,虚拟化(技术)或虚拟技术(英语:Virtualization)是一种资源管理技术,是将计算机的各种实体资源(CPU.内存.磁盘空间.网络适配器 ...
- Javascript 创建对象的三种方法及比较【转载+整理】
https://developer.mozilla.org/zh-CN/docs/JavaScript/Guide/Inheritance_and_the_prototype_chain 本文内容 引 ...
- PHP的异常以及异常存在的意义
php的try catch与其它语言的try catch相比有许多不同,而且用起来相对比较不爽. php中,如果你制作的站点相对较大,同时模块化,并且在错误处理机制上有一套自己的处理机制,可以尝试使用 ...
- ZH奶酪:PHP 执行时间Fatal error: Maximum execution time of...
来源:http://stackoverflow.com/questions/5164930/fatal-error-maximum-execution-time-of-30-seconds-excee ...
- Windows 下 Git 安装与初始配置
官方下载地址:https://git-scm.com/download/win,我下载的最新版是 Git-2.15.1.2-64-bit.exe . Windows 下安装步骤 1.相关信息,直接“ ...