在以前使用xml配置注入的時候, 可以通過name名稱注入, 也可以使用type類型注入.
在springboot中, 可以使用@resource和@autowried注解進行注入.
@resource 默認會使用名稱進行注入, 如果找不到, 會使用自動使用類型進行注入.
@autowried, 則會在容器中尋找匹配的對象, 如果找到則注入成功, 如果沒找到或者找到多個, 則會報錯.
但是, 如果有多個的情況下, 可以使用@qualifier("別名") 進行約束.
1.創建bean
1.1如果使用方法bean注入對象, 給@bean添加name值.
@bean(name="別名")
代碼樣例:
注意
下面的@bean(name = "test01datasource"), 可以使用applicationcontext.getbean("test01datasource") 進行獲取.
@primary表示 如果以type類型進行選擇的話, 首選該bean.
也即是說,使用applicationcontext.getbean(datasource.class) 和applicationcontext.getbean("test01datasource") 獲取到的是同一個對象.
源碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@configuration public class datasourceconfig { @bean (name = "test01datasource" ) @primary @configurationproperties (prefix = "spring.datasource.test01" ) public datasource gettest01datasource() { return datasourcebuilder.create().build(); } @bean (name = "test02datasource" ) @configurationproperties (prefix = "spring.datasource.test02" ) public datasource test02datasource() { return datasourcebuilder.create().build(); } } |
測試代碼:
1
2
3
4
5
6
7
8
|
@autowired applicationcontext applicationcontext; @test public void testdatasourcehashcode() { system.out.println(applicationcontext.getbean(datasource. class ).hashcode()); system.out.println((applicationcontext.getbean( "test01datasource" )).hashcode()); system.out.println((applicationcontext.getbean( "test02datasource" )).hashcode()); } |
結果樣例:
1105282397
1105282397
793657559
1.2使用類注解進行注入對象. @service, @component,添加value值進行創建別名.
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
42
43
44
45
46
47
48
49
50
51
52
53
|
// 創建接口 public interface ibeanservice { public void printinfo(); } //創建實例01, 并且添加上@primary注解, 表名默認使用這個類 @service (value = "bean01" ) @primary public class bean01service implements ibeanservice { @override public void printinfo() { system.out.println( "this is bean 01" ); } } //創建實例02, @service (value = "bean02" ) public class bean02service implements ibeanservice { @override public void printinfo() { system.out.println( "this is bean 02" ); } } @runwith (springrunner. class ) @springboottest public class ibeanservicetest { @autowired applicationcontext applicationcontext; // create default bean. @autowired ibeanservice beanservice; @autowired @qualifier ( "bean01" ) ibeanservice bean01service; @autowired @qualifier ( "bean02" ) ibeanservice bean02service; @test public void printinfo() { // create bean01 ibeanservice bean01 = (ibeanservice) applicationcontext.getbean( "bean01" ); // create bean02 ibeanservice bean02 = (ibeanservice) applicationcontext.getbean( "bean02" ); bean01.printinfo(); bean02.printinfo(); // create default bean, and it is bean01 applicationcontext.getbean(ibeanservice. class ).printinfo(); } @test public void printinfothroughautowired() { beanservice.printinfo(); bean01service.printinfo(); bean02service.printinfo(); } } |
2.調用
2.1.如果需要創建局部變量, 使用applicationcontext.getbean(class/name)創建
對于有@primary的對象, 直接使用getbean(class)進行調用.
applicationcontext.getbean(ibeanservice.class)
對于其他的bean, 使用getbean(name)進行調用, 并進行類型強制轉換.
eg. (ibeanservice) applicationcontext.getbean("bean02");
2.2.如果需要創建成員變量, 使用@autowired和 @qualifier("別名") 進行
對于有@primary的對象, 直接使用@autowired進行調用.代碼如下
1
2
|
@autowired ibeanservice beanservice; |
對于其他的bean, 通過添加@qualifier("別名")進行調用, 代碼如下
1
2
3
|
@autowired @qualifier ( "bean02" ) ibeanservice bean02service; |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/sanpic/article/details/81662034