spring-ioc的使用
ioc容器在很多框架里都在使用,而在spring里它被應(yīng)用的最大廣泛,在框架層面上,很多功能都使用了ioc技術(shù),下面我們看一下ioc的使用方法。
- 把服務(wù)注冊(cè)到ioc容器
- 使用屬性注入反射對(duì)應(yīng)類型的實(shí)例
- 多態(tài)情況下,使用名稱反射類型的實(shí)例
把服務(wù)注冊(cè)到ioc容器
@bean注冊(cè)組件
使用@bean注解進(jìn)行類型的注冊(cè),默認(rèn)你的ioc容器里類型為bean的返回值,名稱為bean所有的方法名,與你的包名稱沒(méi)有直接關(guān)系,如果你的接口有多種實(shí)現(xiàn),在注冊(cè)時(shí)可以使用@bean("lind")這種方式來(lái)聲明。
@component,@configuration,service,repository注冊(cè)組件
這幾個(gè)注解都是在類上面聲明的,而@bean是聲明在方法上的,這一點(diǎn)要注意,這幾個(gè)注解一般是指對(duì)一個(gè)接口的實(shí)現(xiàn),在實(shí)現(xiàn)類上加這些注解,例如,一個(gè)數(shù)據(jù)倉(cāng)儲(chǔ)接口userrepository,它可以有多種數(shù)據(jù)持久化的方式,如sqluserrepositoryimpl和mongouserrepositoryimpl,那么在注冊(cè)時(shí)你需要為他們起一個(gè)別名,如@repository("sql-userrepositoryimpl) qluserrepositoryimpl,默認(rèn)的名稱是類名,但注意類名首字母為小寫(xiě)
。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
public interface emaillogservice { void send(string email, string message); } @component () public class emaillogservicehttpimpl implements emaillogservice { private static final logger logger = loggerfactory.getlogger(emaillogservicehttpimpl. class ); @override public void send(string email, string message) { assert .notnull(email, "email must not be null!" ); logger.info( "send email:{},message:{}" , email, message); } } @component ( "email-socket" ) public class emaillogservicesocketimpl implements emaillogservice { private static final logger logger = loggerfactory.getlogger(emaillogservicesocketimpl. class ); @override public void send(string email, string message) { assert .notnull(email, "email must not be null!" ); logger.info( "send email2:{},message:{}" , email, message); } } // 看一下調(diào)用時(shí)的測(cè)試代碼 @resource (name = "email-socket" ) emaillogservice socketemail; @autowired @qualifier ( "emaillogservicehttpimpl" ) emaillogservice httpemail; @test public void testioc2() { socketemail.send( "ok" , "ok" ); } @test public void testioc1() { httpemail.send( "ok" , "ok" ); } |
在程序中使用bean對(duì)象
1.使用resource裝配bean對(duì)象
在通過(guò)別名
調(diào)用bean時(shí),你可以使用@resource注解來(lái)裝配對(duì)象
2.使用@autowired裝配bean對(duì)象
也可以使用 @autowired
@qualifier( "emaillogservicehttpimpl")兩個(gè)注解去實(shí)現(xiàn)程序中的多態(tài)
。
使用場(chǎng)景
在我們有些相同行為而實(shí)現(xiàn)方式不同的場(chǎng)景中,如版本1接口與版本2接口,在get方法實(shí)現(xiàn)有所不同,而這
兩個(gè)版本都要同時(shí)保留,這時(shí)我們需要遵守開(kāi)閉原則,擴(kuò)展一個(gè)新的接口,而在業(yè)務(wù)上對(duì)代碼進(jìn)行重構(gòu),
提取兩個(gè)版本相同的方法到基類,自己維護(hù)各自獨(dú)有的方法,在為它們的bean起個(gè)名字,在裝配時(shí),通過(guò)
bean的名稱進(jìn)行裝配即可。
寫(xiě)個(gè)偽代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class api_version1(){ @autowired @qualifier ( "print-version1" ) printservice printservice; } class api_version2(){ @autowired @qualifier ( "print-version2" ) printservice printservice; } class baseprintservice{} interface printservice{} @service ( "print-version1" ) class printserviceimplversion1 extends baseprintservice implements printservice{} @service ( "print-version2" ) class printserviceimplversion2 extends baseprintservice implements printservice{} |
以上所述是小編給大家介紹的java的spring-ioc的使用詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:https://www.cnblogs.com/lori/p/10512402.html