傳道、授業(yè)、解惑,何有于我哉!
Java SE / Java ME / Java EE / C / C++ / C#
Spring的文檔上對(duì)Introduction這個(gè)概念和相關(guān)的注解@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).
在這段介紹之后還給出了一個(gè)例子,對(duì)于初學(xué)者要理解這段話以及后面的例子還是蠻困難的,因此下面用一個(gè)簡(jiǎn)單的例子告訴大家什么是Introduction以及如何使用@DeclareParents注解。
對(duì)于Introduction這個(gè)詞,個(gè)人認(rèn)為理解成引入是最合適的,其目標(biāo)是對(duì)于一個(gè)已有的類引入新的接口(有人可能會(huì)問:有什么用呢?簡(jiǎn)單的說,你可以把當(dāng)前對(duì)象轉(zhuǎn)型成另一個(gè)對(duì)象,那么很顯然,你就可以調(diào)用另一個(gè)對(duì)象的方法了),看一個(gè)例子就全明白了。
假設(shè)已經(jīng)有一個(gè)UserService類提供了保存User對(duì)象的服務(wù),但是現(xiàn)在想增加對(duì)User進(jìn)行驗(yàn)證的功能,只對(duì)通過驗(yàn)證的User提供保存服務(wù),在不修改UserService類代碼的前提下就可以通過Introduction來解決。
首先定義一個(gè)Verifier接口,里面定義了進(jìn)行驗(yàn)證的方法validate(),如下所示:
package com.jackfrued.aop;
import com.jackfrued.models.User;
public interface Verifier {
public boolean validate(User user);
}
接下來給出該接口的一個(gè)實(shí)現(xiàn)類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類增加驗(yàn)證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對(duì)象轉(zhuǎn)型為Verifier對(duì)象并對(duì)用戶進(jìn)行驗(yàn)證了,如下所示:
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( " 驗(yàn)證成功 " );
s.serve(user1);
}
}
}
這樣,上面代碼中的user1是不會(huì)被服務(wù)的,當(dāng)然是因?yàn)闆]有通過驗(yàn)證啦!
這樣一說,是不是大概明白什么是Introduction了呢,其實(shí)@DeclareParents用起來也很簡(jiǎn)單吧!
至于配置文件和其他內(nèi)容請(qǐng)參考完整源代碼:/Files/jackfrued/Spring-introduction.rar
主站蜘蛛池模板:
天峨县 |
弥勒县 |
阿勒泰市 |
东宁县 |
镶黄旗 |
水富县 |
平果县 |
绥阳县 |
太谷县 |
乐平市 |
平塘县 |
平罗县 |
博客 |
新竹县 |
长白 |
微博 |
石河子市 |
当涂县 |
忻州市 |
高陵县 |
乌恰县 |
酒泉市 |
湘阴县 |
蓬莱市 |
岢岚县 |
宣汉县 |
慈溪市 |
陵川县 |
阿图什市 |
商洛市 |
肥东县 |
蒲城县 |
高碑店市 |
罗田县 |
张掖市 |
集安市 |
大方县 |
克拉玛依市 |
漳州市 |
孝感市 |
汕尾市 |