好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring多对象引入方法

在以前使用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

查看更多关于Spring多对象引入方法的详细内容...

  阅读:14次