Spring AOP之Introduction(@DeclareParents)简介
2017-01-04 13:07
801 查看
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(),如下所示:
package com.jackfrued.aop;
import com.jackfrued.models.User;
public interface Verifier {
public boolean validate(User user);
}
接下来给出该接口的一个实现类BasicVerifier,如下所示:
package com.jackfrued.aop;
import com.jackfrued.models.User;
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:
package com.jackfrued.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyAspect {
@DeclareParents(value="com.tsinghuait.services.UserService",
defaultImpl=com.tsinghuait.aop.BasicVerifier.class)
public Verifier verifer;
}
接下来就可以将UserService对象转型为Verifier对象并对用户进行验证了,如下所示:
package com.jackfrued.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jackfrued.aop.Verifier;
import com.jackfrued.models.User;
import com.jackfrued.services.Service;
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用起来也很简单吧!
至于配置文件和其他内容请参考完整源代码:/Files/jackfrued/Spring-introduction.rar
由此也就明白了@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(),如下所示:
package com.jackfrued.aop;
import com.jackfrued.models.User;
public interface Verifier {
public boolean validate(User user);
}
接下来给出该接口的一个实现类BasicVerifier,如下所示:
package com.jackfrued.aop;
import com.jackfrued.models.User;
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:
package com.jackfrued.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyAspect {
@DeclareParents(value="com.tsinghuait.services.UserService",
defaultImpl=com.tsinghuait.aop.BasicVerifier.class)
public Verifier verifer;
}
接下来就可以将UserService对象转型为Verifier对象并对用户进行验证了,如下所示:
package com.jackfrued.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jackfrued.aop.Verifier;
import com.jackfrued.models.User;
import com.jackfrued.services.Service;
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用起来也很简单吧!
至于配置文件和其他内容请参考完整源代码:/Files/jackfrued/Spring-introduction.rar
由此也就明白了@DeclareParents的作用,就是不改变原有类的情况下进行扩展(比如无源码)
相关文章推荐
- SSH商城Demo --- 一、商城简介、需求
- java中请求响应时间监控
- 玩转spring boot——结合JPA事务
- Java设计模式之——观察者模式(Observer)
- springmvc无法接收uploadify插件上传的数据
- 论:开发者信仰之“天下IT是一家“(Java .NET篇)
- 2016,Java成神初年
- java性能调优及问题追踪--Btrace的使用
- JAVA设计模式之适配器模式
- javaweb开发过程中小工具系列之支持事务的JdbcUtils
- org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dia
- eventloop & actor模式 & Java线程模型演进 & Netty线程模型 总结
- eclipse修改后运行的还是之前的代码
- spring-boot 连接池 druid 的配置及监控
- Java中的instanceof关键字
- java多线程系类:基础篇:08之join
- 利用JDK(1.6及以上版本)创建WebService
- 利用JDK(1.6及以上版本)创建WebService
- java transient简介
- HV000030: No validator could be found for type: java.lang.Integer