好得很程序员自学网

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

Spring注入Bean的一些方式总结

通过注解 注入 bean

背景

我们谈到spring的时候一定会提到ioc容器、di依赖注入,spring通过将一个个类标注为bean的方法注入到ioc容器中,达到了控制反转的效果。那么我们刚开始接触bean的时候,一定是使用xml文件,一个一个的注入,就例如下面这样。

?

1

<bean id= "bean" class = "beandemo.bean" />

我们的项目一般很大的话,就需要成千上百个bean去使用,这样写起来就很繁琐。那么spring就帮我们实现了一种通过注解来实现注入的方法。只需要在你需要注入的类前面加上相应的注解,spring就会帮助我们扫描到他们去实现注入。

xml扫描包的方式

?

1

<context:component-scan base- package = "com测试数据pany.beandemo" />

通过注解注入的一般形式

一般情况下,注入bean有一个最直白,最易懂的方式去实现注入,下面废话先不多说,先贴代码。

bean类

?

1

2

public class mybean{

}

configuration类

?

1

2

3

4

5

6

7

8

9

    //创建一个class配置文件

    @configuration

    public class myconfiguration{

        //将一个bean交由spring进行管理

@bean

public mybean mybean(){

  return new mybean();

}

    }

test类

与xml有一点不同,这里在test中,实例化的不再是classpathxmlapplicationcontext,而是获取的annotationconfigapplicationcontext实例。

?

1

2

3

applicationcontext context = new annotationconfigapplicationcontext(myconfiguration. class );

mybean mybean = cotext.getbean( "mybean" ,mybean. class );

system.out.println( "mybean = " + mybean);

上面的代码中mybean也就是我们需要spring去管理的一个bean,他只是一个简单的类。而myconfiguration中,我们首先用@configuration注解去标记了该类,这样标明该类是一个spring的一个配置类,在加载配置的时候会去加载他。

在myconfiguration中我们可以看到有一个方法返回的是一个mybean的实例,并且该方法上标注着@bean的注解,标明这是一个注入bean的方法,会将下面的返回的bean注入ioc。

通过构造方法注入bean

我们在生成一个bean实例的时候,可以使用bean的构造方法将bean实现注入。直接看代码

bean类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

@component

public class mybeanconstructor {

 

private anotherbean anotherbeanconstructor;

 

@autowired

public mybeanconstructor(anotherbean anotherbeanconstructor){

  this .anotherbeanconstructor = anotherbeanconstructor;

}

 

@override

public string tostring() {

return "mybean{" +

"anotherbeanconstructor=" + anotherbeanconstructor +

'}' ;

}

}

anotherbean类

?

1

2

3

@component (value= "bean的id,默认为类名小驼峰" )

public class anotherbean {

}

configuration类

?

1

2

3

4

@configuration

@componentscan ( "com测试数据pany.annotationbean" )

public class myconfiguration{

}

这里我们可以发现,和一般方式注入的代码不一样了,我们来看看新的注解都是什么意思:

@autowired

简单粗暴,直接翻译过来的意思就是自动装配:wrench:,还不理解为什么叫自动装配:wrench:?看了下一个注解的解释你就知道了。若是在这里注入的时候指定一个bean的id就要使用@qualifier注解

@component(默认单例模式)

什么??这翻译过来是零件,怎么感觉像是修汽车??是的,spring管理bean的方法就是修汽车的方式。我们在需要将一个类变成一个bean被spring可以注入的时候加上注解零件@conmonent,那么我们就可以在加载bean的时候把他像零件一样装配:wrench:到这个ioc汽车上了
在这里我们还有几个其他的注解也可以实现这个功能,也就是细化的@component:

@controller 标注在controller层 @service 标注在service层 @repository 标注在dao层 @componentscan("")

还是翻译,零件扫描,我们去看看括号里的[零件仓库]里面,哪些[零件](类)需要被装载,spring就会去扫描这个包,将里面所有标注了@component的类进行注入。

这里的通过构造方法进行注入就很好理解了,我们在装配mybean这个零件的时候,突然发现他必须在anotherbean的基础上才能安装到ioc里面,那么我们就在每次装配mybean的时候自动装配:wrench:一个anotherbean进去。举个:chestnut:吧:

还是以汽车为例,我们在踩油门出发之前,是不是必须发车??这里的autowired的内容就像发车,你不发车,这个油门你踩断都没有用,他都不会走。

通过set方法注入bean

我们可以在一个属性的set方法中去将bean实现注入,看代码吧

mybean类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

@component

public class mybeanset {

 

private anotherbean anotherbeanset;

 

@autowired

public void setanotherbeanset(anotherbean anotherbeanset) {

this .anotherbeanset = anotherbeanset;

}

 

@override

public string tostring() {

return "mybeanset{" +

"anotherbeanset=" + anotherbeanset +

'}' ;

}

}

configuration类 和 test类

同上一个,就不贴了

这里我们发现在setter方法上我们有一个@autowired,与上面不同的是,我们不会在实例化该类时就自动装配:wrench:这个对象,而是在显式调用setter的时候去装配。

通过属性去注入bean

我们前面两种注入的方式诸如时间不同,并且代码较多,若是通过属性,即就是

?

1

2

3

4

5

6

7

8

9

10

11

12

13

@component

public class mybeanproperty {

 

@autowired

private anotherbean anotherbeanproperty;

 

@override

public string tostring() {

return "mybeanproperty{" +

"anotherbeanproperty=" + anotherbeanproperty +

'}' ;

}

}

这里我们可以看到我们这个类中需要使用anotherbean这个实例对象,我们可以通过@autowired去自动装配它。

对于有些小伙伴问私有属性,spring怎么去加载它到ioc的?推荐去看看反射

通过list注入bean

mybeanlist类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

@component

public class mybeanlist {

 

private list<string> stringlist;

 

@autowired

public void setstringlist(list<string> stringlist) {

this .stringlist = stringlist;

}

 

public list<string> getstringlist() {

return stringlist;

}

}

myconfiguration类

?

1

2

3

4

5

6

7

8

9

10

11

12

@configuration

@componentscan ( "annobean.annotationbean" )

public class myconfiguration {

 

  @bean

  public list<string> stringlist(){

   list<string> stringlist = new arraylist<string>();

   stringlist.add( "list-1" );

   stringlist.add( "list-2" );

   return stringlist;

  }

}

这里我们将mybeanlist进行了注入,对list中的元素会逐一注入。下面介绍另一种方式注入list

myconfiguration类

?

1

2

3

4

5

6

7

8

9

10

11

12

    @bean

//通过该注解设定bean注入的优先级,不一定连续数字

@order ( 34 )

public string string1(){

  return "string-1" ;

}

 

@bean

@order ( 14 )

public string string2(){

  return "string-2" ;

}

注入与list中泛型一样的类型,会自动去匹配类型,及时这里没有任何list的感觉,只是string的类型,但他会去通过list的bean的方式去注入。

第二种方式的优先级高于第一种,当两个都存在的时候,若要强制去使用第一种方式,则要去指定bean的id即可

通过map去注入bean

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

@component

public class mybeanmap {

 

private map<string,integer> integermap;

 

public map<string, integer> getintegermap() {

  return integermap;

}

 

@autowired

public void setintegermap(map<string, integer> integermap) {

  this .integermap = integermap;

}

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

    @bean

public map<string,integer> integermap(){

  map<string,integer> integermap = new hashmap<string, integer>();

  integermap.put( "map-1" , 1 );

  integermap.put( "map-2" , 2 );

  return integermap;

}

 

@bean

public integer integer1(){

  return 1 ;

}

 

@bean

public integer integer2(){

  return 2 ;

}

同样这里也具有两种方式去注入map类型bean,且第二种的优先值高于第一种

以上就是bean通过注解注入的几种方式,大家可以对比着xml注入的方式去看。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。

原文链接:https://juejin.im/post/5ca81a536fb9a05e6538aa39

查看更多关于Spring注入Bean的一些方式总结的详细内容...

  阅读:12次