春天中怎么向一个单例bean中注入非单例bean

  介绍

这篇文章主要介绍“春中怎么向一个单例bean中注入非单例bean”,在日常操作中,相信很多人在春天中怎么向一个单例bean中注入非单例bean问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答“春中怎么向一个单例bean中注入非单例bean”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

<编辑类="目录">目录 <李>

前言

<李>

错误实例演示

<李>

实现ApplicationContextAware接口

<李>

查找方法

<李>

查找方法签名


前言

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

@ component   {public  class 人      @ autowired   private  Man 人;   }

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

错误实例演示

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

package  com.example.myDemo.component;      import  org.springframework.context.annotation.Scope;   import  org.springframework.stereotype.Component;      @ component   @Scope (=value “prototype")   public  class  Man , {      ,,,public  void 吃(),{   ,,,,,,,System.out.println(“小姐;like  beef");   ,,,}   }

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

package  com.example.myDemo.component;      import  org.springframework.beans.factory.annotation.Autowired;   import  org.springframework.beans.factory.annotation.Lookup;   import  org.springframework.stereotype.Component;      @ component   public  class  Woman  {   ,,,//使用依赖注入的方式,注入原型的Man ,, @ autowired   ,,,private  Man 人;      ,,,public  void 吃(),{   ,,,,,,,System.out.println(“男人:“+人);   ,,,,,,,System.out.println(“小姐;like  fruits");   ,,,}      }

下面看测试方法,

package  com.example.myDemo;      import  com.example.myDemo.component.MyFactoryBean;   import  com.example.myDemo.component.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(排除={DataSourceAutoConfiguration.class,, HibernateJpaAutoConfiguration.class})   public  class  MyDemoApplication  {      ,,,public  static  void  main (String [], args), {   ,,,,,,,ApplicationContext  ac=SpringApplication.run (MyDemoApplication.class, args);      ,,,,,,,Woman 女人=(女人)ac.getBean (“woman");   ,,,,,,,,(int 我=0;i<5,我+ +){   ,,,,,,,,,,,woman.eat ();   ,,,,,,,}         ,,,}      }

看下测试结果,

春天中怎么向一个单例bean中注入非单例bean

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

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

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

实现ApplicationContextAware接口

都知道ApplicationContextAware接口是春天提供的一个扩展点,实现该接口的类可以获得ApplicationContext

Woamn类改成下面的样子

package  com.example.myDemo.component;      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.Component;      @ component   public  class  Woman  implements  ApplicationContextAware  {      ,,,private  Man 人;      ,,,private  ApplicationContext 交流;      ,,,public  void 吃(),{   ,,,,,,,this.man =,(男人),ac.getBean (“man");   ,,,,,,,System.out.println(“男人:“,+,男人);   ,,,,,,,System.out.println(“小姐;like  fruits");   ,,,}      ,,@Override   ,,,public  void  setApplicationContext (ApplicationContext  applicationContext), throws  BeansException  {   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

春天中怎么向一个单例bean中注入非单例bean