欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring中向一個(gè)單例bean中注入非單例bean的方法詳解

 更新時(shí)間:2021年07月18日 14:26:49   作者:北漂程序員  
Spring是先將Bean對(duì)象實(shí)例化之后,再設(shè)置對(duì)象屬性,所以會(huì)先調(diào)用他的無參構(gòu)造函數(shù)實(shí)例化,每個(gè)對(duì)象存在一個(gè)map中,當(dāng)遇到依賴,就去map中調(diào)用對(duì)應(yīng)的單例對(duì)象,這篇文章主要給大家介紹了關(guān)于spring中向一個(gè)單例bean中注入非單例bean的相關(guān)資料,需要的朋友可以參考下

前言

看到這個(gè)題目相信很多小伙伴都是懵懵的,平時(shí)我們的做法大都是下面的操作

@Component
public class People{

@Autowired
private Man man;
}

這里如果Man是單例的,這種寫法是沒有問題的,但如果Man是原型的,這樣是否會(huì)存在問題。

錯(cuò)誤實(shí)例演示

這里有一個(gè)原型(生命周期為prototype)的類

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 eat() {
        System.out.println("I like beef");
    }
}

有一個(gè)單例(生命周期為singleton)的類

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 man;

    public void eat() {
        System.out.println("man:"+man);
        System.out.println("I like fruits");
    }

}

下面看測(cè)試方法,

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(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();
        }


    }

}

看下測(cè)試結(jié)果,

上面的結(jié)果顯示W(wǎng)oman中的man是單例的,因?yàn)?次循環(huán)打印打出的結(jié)果是同一個(gè)對(duì)象,發(fā)生了什么,

Woman是單例的,Man是原型的,我們使用常規(guī)的@Autowired注解注入的卻是同一個(gè)實(shí)例,這里想下為什么Man是一個(gè)對(duì)象,Woman是單例的,意味著在整個(gè)spring容器中只有一個(gè)實(shí)例,在屬性注入的時(shí)候肯定也只會(huì)注入一次,所以其中Man屬性也只能是一個(gè)實(shí)例,出現(xiàn)上圖的結(jié)果也就不稀奇了。

現(xiàn)在有這樣一個(gè)需求要向單例bean中注入原型bean,要怎么實(shí)現(xiàn)這樣的需求

實(shí)現(xiàn)ApplicationContextAware接口

都知道ApplicationContextAware接口是spring提供的一個(gè)擴(kuò)展點(diǎn),實(shí)現(xiàn)該接口的類可以獲得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 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實(shí)現(xiàn)了ApplicationContextAware接口,注入了ApplicaitonContext對(duì)象,然后再eat()方法中通過AppicationContext獲得Man的實(shí)例,看測(cè)試結(jié)果,

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

思考下為什么采用這種方式可以達(dá)到注入原型bean的目的

在eat()方法中使用ApplicationContext的getBean方法獲取Man,eat()方法每執(zhí)行一次均會(huì)調(diào)用一次getBean方法,getbean方法在執(zhí)行的時(shí)候的時(shí)候會(huì)判斷Man的生命周期,如果是原型(prototype)的,那么每調(diào)用一次就會(huì)重新實(shí)例化一個(gè)Man,所以會(huì)出現(xiàn)上述的結(jié)果。

該方法有一個(gè)很大的缺點(diǎn)那就是和spring耦合度太高,不符合降低系統(tǒng)的耦合度的要求。

lookup method

spring也考慮了向一個(gè)單例bean中注入原型bean的情況,提供了@Lookup注解,在XML配置方式下是<lookup-method>標(biāo)簽,這里僅使用注解的方式演示,

Woman類修改如下,

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  {

    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;
    }

}

看下測(cè)試結(jié)果,

上圖顯示man是一個(gè)多例的,也就是向單例bean中注入了原型bean,其作用的是@Lookup注解。

通過@Lookup注解便完成了注入原型bean的目的,留個(gè)思考問題spring是如何做到的?

lookup method簽名

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

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

  • 方法可以是public也可以是protected;
  • 方法可以是抽象的也可以是非抽象的;
  • 方法的返回值是要注入的類型,這里是prototype類型的類;
  • 方法沒有入?yún)ⅲ?/li>
  • 方法體可以是空的。具體返回值可以是null或任何類型,對(duì)結(jié)果沒有影響;

總結(jié)

分享了向單例bean中注入原型bean的方式,使用lookup的方式會(huì)更簡(jiǎn)潔些。

這還可能是道面試題哦,各位小伙伴注意嘍。lookup的原理下次分享,敬請(qǐng)關(guān)注

到此這篇關(guān)于spring中向一個(gè)單例bean中注入非單例bean的文章就介紹到這了,更多相關(guān)spring注入非單例bean內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論