好得很程序员自学网

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

spring中向一个单例bean中注入非单例bean的方法详解

前言

看到这个题目相信很多小伙伴都是懵懵的,平时我们的做法大都是下面的操作

?

1

2

3

4

5

6

@component

public class people{

 

@autowired

private man man;

}

这里如果man是单例的,这种写法是没有问题的,但如果man是原型的,这样是否会存在问题。

错误实例演示

这里有一个原型(生命周期为prototype)的类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

package com.example.mydemo测试数据ponent;

 

import org.springframework.context.annotation.scope;

import org.springframework.stereotype测试数据ponent;

 

@component

@scope (value = "prototype" )

public class man  {

 

     public void eat() {

         system.out.println( "i like beef" );

     }

}

有一个单例(生命周期为singleton)的类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

package com.example.mydemo测试数据ponent;

 

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.beans.factory.annotation.lookup;

import org.springframework.stereotype测试数据ponent;

 

@component

public class woman {

     //使用依赖注入的方式,注入原型的man    @autowired

     private man man;

 

     public void eat() {

         system.out.println( "man:" +man);

         system.out.println( "i like fruits" );

     }

 

}

下面看测试方法,

?

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

package com.example.mydemo;

 

import com.example.mydemo测试数据ponent.myfactorybean;

import com.example.mydemo测试数据ponent.woman;

import com.example.mydemo.po.student;

import org.springframework.boot.springapplication;

import org.springframework.boot.autoconfigure.springbootapplication;

import org.springframework.boot.autoconfigure.jdbc.datasourceautoconfiguration;

import org.springframework.boot.autoconfigure.orm.jpa.hibernatejpaautoconfiguration;

import org.springframework.context.applicationcontext;

 

@springbootapplication (exclude={datasourceautoconfiguration. class , hibernatejpaautoconfiguration. class })

public class mydemoapplication {

 

     public static void main(string[] args) {

         applicationcontext ac=springapplication.run(mydemoapplication. class , args);

 

         woman woman=(woman)ac.getbean( "woman" );

         for ( int i= 0 ;i< 5 ;i++){

             woman.eat();

         }

 

 

     }

 

}

看下测试结果,

上面的结果显示woman中的man是单例的,因为5次循环打印打出的结果是同一个对象,发生了什么,

woman是单例的,man是原型的,我们使用常规的@autowired注解注入的却是同一个实例,这里想下为什么man是一个对象,woman是单例的,意味着在整个spring容器中只有一个实例,在属性注入的时候肯定也只会注入一次,所以其中man属性也只能是一个实例,出现上图的结果也就不稀奇了。

现在有这样一个需求要向单例bean中注入原型bean,要怎么实现这样的需求

实现applicationcontextaware接口

都知道applicationcontextaware接口是spring提供的一个扩展点,实现该接口的类可以获得applicationcontext

woamn类改成下面的样子

?

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

package com.example.mydemo测试数据ponent;

 

import org.springframework.beans.beansexception;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.beans.factory.annotation.lookup;

import org.springframework.context.applicationcontext;

import org.springframework.context.applicationcontextaware;

import org.springframework.stereotype测试数据ponent;

 

@component

public class woman implements applicationcontextaware {

 

     private man man;

 

     private applicationcontext ac;

 

     public void eat() {

         this .man = (man) ac.getbean( "man" );

         system.out.println( "man:" + man);

         system.out.println( "i like fruits" );

     }

 

     @override

     public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception {

         this .ac = applicationcontext;

     }

}

woman实现了applicationcontextaware接口,注入了applicaitoncontext对象,然后再eat()方法中通过appicationcontext获得man的实例,看测试结果,

可以看到man属性是多例的也就是符合原型模式的定义。

思考下为什么采用这种方式可以达到注入原型bean的目的

在eat()方法中使用applicationcontext的getbean方法获取man,eat()方法每执行一次均会调用一次getbean方法,getbean方法在执行的时候的时候会判断man的生命周期,如果是原型(prototype)的,那么每调用一次就会重新实例化一个man,所以会出现上述的结果。

该方法有一个很大的缺点那就是和spring耦合度太高,不符合降低系统的耦合度的要求。

lookup method

spring也考虑了向一个单例bean中注入原型bean的情况,提供了@lookup注解,在xml配置方式下是<lookup-method>标签,这里仅使用注解的方式演示,

woman类修改如下,

?

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

package com.example.mydemo测试数据ponent;

 

import org.springframework.beans.beansexception;

import org.springframework.beans.factory.annotation.autowired;

import org.springframework.beans.factory.annotation.lookup;

import org.springframework.context.applicationcontext;

import org.springframework.context.applicationcontextaware;

import org.springframework.stereotype测试数据ponent;

 

@component

public class woman  {

 

     private man man;

 

     public void eat() {

         this .man = createman();

         system.out.println( "man:" + man);

         system.out.println( "i like fruits" );

     }

 

     @lookup

     public man createman(){

         return null ;

     }

 

}

看下测试结果,

上图显示man是一个多例的,也就是向单例bean中注入了原型bean,其作用的是@lookup注解。

通过@lookup注解便完成了注入原型bean的目的,留个思考问题spring是如何做到的?

lookup method签名

被@lookup注解或<lookup-method>配置的方法有如下要求,

  public|protected [abstract] return-type methodname(no-argments)

方法可以是public也可以是protected; 方法可以是抽象的也可以是非抽象的; 方法的返回值是要注入的类型,这里是prototype类型的类; 方法没有入参; 方法体可以是空的。具体返回值可以是null或任何类型,对结果没有影响;

总结

分享了向单例bean中注入原型bean的方式,使用lookup的方式会更简洁些。

这还可能是道面试题哦,各位小伙伴注意喽。lookup的原理下次分享,敬请关注

到此这篇关于spring中向一个单例bean中注入非单例bean的文章就介绍到这了,更多相关spring注入非单例bean内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://HdhCmsTestcnblogs测试数据/teach/p/15026142.html

查看更多关于spring中向一个单例bean中注入非单例bean的方法详解的详细内容...

  阅读:20次