亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

spring中的注入list集合

 更新時間:2022年11月21日 08:47:13   作者:小小少年_  
這篇文章主要介紹了spring中的注入list集合問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

spring在幫我們管理bean的時候,會幫我們完成自動注入,其中有一個比較特殊的類型:list

這篇筆記主要記錄spring注入list集合的原理

spring注入list集合

應用

public interface Rest {

}

@Component
public class RestServiceImpl01 implements Rest{

}

@Component
public class RestServiceImpl02 implements Rest{

}

@Component
public class OrderService {
? ? @Autowired
? ? //@Qualifier
? ? private List<Rest> restList;

? ? public void test() {
? ? ? ? System.out.println("打印注入的集合的值,restList:" + restList);
? ? }
}

public class Test {

? ? public static void main(String[] args) {
? ? ? ? AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
? ? ? ? OrderService orderService = ac.getBean(OrderService.class);
? ? ? ? orderService.test();
? ? }
}

以上代碼執(zhí)行之后,打印的結果是:

打印注入的集合的值,restList:[com.spring.list.RestServiceImpl01@60611244, com.spring.list.RestServiceImpl02@3745e5c6]

spring中,在使用@Autowired注解注入list集合的時候,并不會根據List類型去容器中查找,而是根據list集合的元素類型,從spring容器中找到所有的實現類,放在list集合中,然后注入到bean中

那如果我們想要指定只注入部分bean怎么辦呢?

@Component
public class OrderService {
? ? @Autowired
? ? @Qualifier
? ? private List<Rest> restList;

? ? public void test() {
? ? ? ? System.out.println("打印注入的集合的值,restList:" + restList);
? ? }
}

只需要把這的@Qualifier注解放開,然后在需要注入的bean上,加上這個注解

@Component
@Qualifier
public class RestServiceImpl02 implements Rest{

}

此時再運行代碼:打印注入的集合的值,restList:[com.spring.list.RestServiceImpl02@d706f19]

所以這就是注入list集合bean的應用

原理

對于bean的注入,如果我們使用的是@Autowired注解,會被AutowiredAnnotationBeanPostProcessor處理

鏈路:

org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#postProcessProperties
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement#inject
            org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement#resolveFieldValue
    org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveDependency
        org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveDependency

在doResolveDependency方法中,有一個代碼邏輯

Object multipleBeans = resolveMultipleBeans(descriptor, beanName, autowiredBeanNames, typeConverter);
if (multipleBeans != null) {
?? ?return multipleBeans;
}

這里就是來解析list類型的

org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveMultipleBeans
else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
?? ?Class<?> elementType = descriptor.getResolvableType().asCollection().resolveGeneric();
?? ?if (elementType == null) {
?? ??? ?return null;
?? ?}
?? ?// 在這里會取解析list集合中指定的接口所有的實現類
?? ?Map<String, Object> matchingBeans = findAutowireCandidates(beanName, elementType,
?? ??? ??? ?new MultiElementDescriptor(descriptor));
?? ?if (matchingBeans.isEmpty()) {
?? ??? ?return null;
?? ?}
?? ?if (autowiredBeanNames != null) {
?? ??? ?autowiredBeanNames.addAll(matchingBeans.keySet());
?? ?}
?? ?TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
?? ?Object result = converter.convertIfNecessary(matchingBeans.values(), type);
?? ?if (getDependencyComparator() != null && result instanceof List) {
?? ??? ?((List<?>) result).sort(adaptDependencyComparator(matchingBeans));
?? ?}
?? ?return result;
}

在這個方法中,這段代碼是來解析list集合的,所以只截取了這一部分代碼,這一部分關鍵的代碼是:findAutowireCandidates方法

protected Map<String, Object> findAutowireCandidates(
?? ??? ?@Nullable String beanName, Class<?> requiredType, DependencyDescriptor descriptor) {

?? ?/**
?? ? * 根據類型,獲取當前beanDefinitionMap中的beanName,注意:這里是從beanDefinitionMap中獲取的,并不是直接從spring
?? ? * 容器中獲取
?? ? * 獲取到的是待注入bean的name
?? ? */
?? ?String[] candidateNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
?? ??? ??? ?this, requiredType, true, descriptor.isEager());
?? ?Map<String, Object> result = new LinkedHashMap<>(candidateNames.length);
?? ?/**
?? ? * resolvableDependencies:這里來遍歷這個集合,判斷要注入的bean是否是該類型的
?? ? * resolvableDependencies這個集合,默認有四個值
?? ? * interface org.springframework.context.ApplicationContext" -> {AnnotationConfigApplicationContext@1641}
?? ? * interface org.springframework.beans.factory.BeanFactory" -> {DefaultListableBeanFactory@1630}
?? ? * interface org.springframework.core.io.ResourceLoader" -> {AnnotationConfigApplicationContext@1641}?
?? ? * interface org.springframework.context.ApplicationEventPublisher -> {AnnotationConfigApplicationContext@1641}
?? ? */
?? ?for (Class<?> autowiringType : this.resolvableDependencies.keySet()) {
?? ??? ?if (autowiringType.isAssignableFrom(requiredType)) {
?? ??? ??? ?Object autowiringValue = this.resolvableDependencies.get(autowiringType);
?? ??? ??? ?autowiringValue = AutowireUtils.resolveAutowiringValue(autowiringValue, requiredType);
?? ??? ??? ?if (requiredType.isInstance(autowiringValue)) {
?? ??? ??? ??? ?result.put(ObjectUtils.identityToString(autowiringValue), autowiringValue);
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?/**
?? ? * 確切的說,是在isAutowireCandidate里面對Qualifier注解進行了判斷
?? ? * ?org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver#isAutowireCandidate(org.springframework.beans.factory.config.BeanDefinitionHolder, org.springframework.beans.factory.config.DependencyDescriptor)
?? ? */
?? ?for (String candidate : candidateNames) {
?? ??? ?if (!isSelfReference(beanName, candidate) && isAutowireCandidate(candidate, descriptor)) {
?? ??? ??? ?addCandidateEntry(result, candidate, descriptor, requiredType);
?? ??? ?}
?? ?}
?? ?if (result.isEmpty() && !indicatesMultipleBeans(requiredType)) {
?? ??? ?// Consider fallback matches if the first pass failed to find anything...
?? ??? ?DependencyDescriptor fallbackDescriptor = descriptor.forFallbackMatch();
?? ??? ?for (String candidate : candidateNames) {
?? ??? ??? ?if (!isSelfReference(beanName, candidate) && isAutowireCandidate(candidate, fallbackDescriptor)) {
?? ??? ??? ??? ?addCandidateEntry(result, candidate, descriptor, requiredType);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?if (result.isEmpty()) {
?? ??? ??? ?// Consider self references as a final pass...
?? ??? ??? ?// but in the case of a dependency collection, not the very same bean itself.
?? ??? ??? ?for (String candidate : candidateNames) {
?? ??? ??? ??? ?if (isSelfReference(beanName, candidate) &&
?? ??? ??? ??? ??? ??? ?(!(descriptor instanceof MultiElementDescriptor) || !beanName.equals(candidate)) &&
?? ??? ??? ??? ??? ??? ?isAutowireCandidate(candidate, fallbackDescriptor)) {
?? ??? ??? ??? ??? ?addCandidateEntry(result, candidate, descriptor, requiredType);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?return result;
}

在源碼中,就是在isAutowireCandidate()這個方法中,對@Qualifier注解進行的過濾,也就是說,如果我們在注入list集合的時候,沒有添加@Qualifier注解,那這個方法都會返回true,然后將所有的實現類都返回如果加了@Qualifier注解,這里只有加了@Qualifier注解的實現類會返回TRUE,會被返回

這個方法的實現細節(jié),待研究,debug看源碼的時候,看到這樣的結果

小結

所以,在spring中,我們在注入list集合的時候,如果只加了@Autowired注解,那就會把集合元素的所有實現類都注入進來,如果想只注入指定的類,那就使用@Qualifier注解

spring集合注入的幾種方式

什么是集合注入

通俗的來講就是在beans.xml文件中,通過集合的方式來進行賦值,我們在Java基礎中學過通過集合的方式來進行賦值

集合注入的幾種方式

Spring提供了以下四種集合類的配置元素

1、list 該標簽用來裝配可重復的list值

2、set 該標簽用來裝配沒有重復的set值

3、map 該標簽可用來注入鍵和值可以為任何類型的鍵值對

4、props 該標簽支持注入鍵和值都是字符串類型的鍵值對

簡單的配置代碼實現

1、Programmer類的創(chuàng)建

package com.model;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Programmer {
    private List<String> cars;
    private Set<String> pats; //寵物
    private Map<String,String> infos; //信息
    private Properties mysqlInfos; //mysql數據庫鏈接信息
    private String[] numbers; //家庭成員

    public List<String> getCars() {
        return cars;
    }

    public void setCars(List<String> cars) {
        this.cars = cars;
    }

    public Set<String> getPats() {
        return pats;
    }

    public void setPats(Set<String> pats) {
        this.pats = pats;
    }

    public Map<String, String> getInfos() {
        return infos;
    }

    public void setInfos(Map<String, String> infos) {
        this.infos = infos;
    }

    public Properties getMysqlInfos() {
        return mysqlInfos;
    }

    public void setMysqlInfos(Properties mysqlInfos) {
        this.mysqlInfos = mysqlInfos;
    }

    public String[] getNumbers() {
        return numbers;
    }

    public void setNumbers(String[] numbers) {
        this.numbers = numbers;
    }
}

2、beans.xml文件中的配置,集合注入

<?xml version="1.0" encoding="utf-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--
    集合注入
    -->
    <bean id="programmer" class="com.model.Programmer">
        <property name="cars">
            <!-- 1.  list數據注入 //有序集合-->
            <list>
                <value>ofo</value>
                <value>mobai</value>
                <value>寶馬</value>
            </list>
        </property>

        <property name="pats">
            <!-- 2. set數據注入 //無序集合-->
            <set>
                <value>小黑</value>
                <value>小紅</value>
                <value>小白</value>
            </set>
        </property>

        <property name="infos">
            <!-- 3. map數據注入 -->
            <map>
                <entry key="name" value="cjx"></entry>
                <entry key="age" value="23"></entry>
                <entry key="id" value="20821111355"></entry>
            </map>
        </property>

        <property name="mysqlInfos">
            <!-- 4. properties數據注入  //實際也是set類型是無序的-->
            <props>
                <prop key="url">mysql:jdbc://localhost:3306/dbname</prop>
                <prop key="user">root</prop>
                <prop key="password">123456</prop>
            </props>
        </property>

        <property name="numbers">
            <!-- 5. 數組的數據注入 -->
            <array>
                <value>哥哥</value>
                <value>弟弟</value>
                <value>妹妹</value>
                <value>姐姐</value>
            </array>
        </property>
    </bean>
</beans>

3、創(chuàng)建Lesson測試

public class Lesson4 {

    @Test
    public void test() throws Exception{

        /*
        * bean的集合注入
        * */
        ApplicationContext context = new ClassPathXmlApplicationContext("beans4.xml");
        Programmer programmer = (Programmer) context.getBean("programmer");
       
       
	 	System.out.println("車:"+programmer.getCars());
        System.out.println("寵物:"+programmer.getPats());
        System.out.println("信息:"+programmer.getInfos());
        System.out.println("數據庫連接信息::"+programmer.getMysqlInfos());
        System.out.println("家庭成員:");
        //家庭成員是數組類型,需要遍歷
        for (String number: programmer.getNumbers()){
            System.out.println(number);
        }
    }
}

4、測試運行結果

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • java項目依賴包選擇具體實現類示例介紹

    java項目依賴包選擇具體實現類示例介紹

    這篇文章主要為大家介紹了java項目依賴包選擇具體實現類示例介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • 詳解SpringBoot下文件上傳與下載的實現

    詳解SpringBoot下文件上傳與下載的實現

    這篇文章主要介紹了SpringBoot下文件上傳與下載的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • SSM項目中配置LOG4J日志的方法

    SSM項目中配置LOG4J日志的方法

    本篇文章主要介紹了SSM項目中配置LOG4J日志的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • java中charAt()方法的使用及說明

    java中charAt()方法的使用及說明

    這篇文章主要介紹了java中charAt()方法的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • NoHttpResponseException問題分析解決記錄

    NoHttpResponseException問題分析解決記錄

    這篇文章主要為大家介紹了NoHttpResponseException問題分析解決記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • Java多種經典排序算法(含動態(tài)圖)

    Java多種經典排序算法(含動態(tài)圖)

    排序算法是老生常談的了,但是在面試中也有會被問到,例如有時候,在考察算法能力的時候,不讓你寫算法,就讓你描述一下,某個排序算法的思想以及時間復雜度或空間復雜度。我就遇到過,直接問快排的,所以這次我就總結梳理一下經典的十大排序算法以及它們的模板代碼
    2021-04-04
  • Spring?Boot如何接入Security權限認證服務

    Spring?Boot如何接入Security權限認證服務

    Spring Security?是一個高度可定制的身份驗證和訪問控制的框架,提供了完善的認證機制和方法級的授權功能,本文通過案例將Spring Security整合到SpringBoot中,要實現的功能就是在認證服務器上登錄,然后獲取Token,再訪問資源服務器中的資源,感興趣的朋友一起看看吧
    2024-07-07
  • IDEA 快速返回上次查看代碼的位置的方法

    IDEA 快速返回上次查看代碼的位置的方法

    這篇文章主要介紹了IDEA 快速返回上次查看代碼的位置的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • 詳解Java異常處理中finally子句的運用

    詳解Java異常處理中finally子句的運用

    這篇文章主要介紹了Java異常處理中finally子句的運用,是Java入門學習中的基礎知識,需要的朋友可以參考下
    2015-09-09
  • Java面向接口編程之簡單工廠模式示例

    Java面向接口編程之簡單工廠模式示例

    這篇文章主要介紹了Java面向接口編程之簡單工廠模式,結合實例形式詳細分析了java面向接口編程簡單工廠模式的具體定義與使用方法,需要的朋友可以參考下
    2019-09-09

最新評論