首页 > 编程语言 >Spring源码分析:List集合注入

Spring源码分析:List集合注入

时间:2024-05-15 15:42:47浏览次数:22  
标签:process Spring List springframework spring 源码 org import public

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>spring-boot-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <spring-boot.version>2.4.2</spring-boot.version>
        <spring-cloud.version>2020.0.1</spring-cloud.version>
        <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>

        <knife4j.version>4.1.0</knife4j.version>
        <hutool.version>5.6.1</hutool.version>
        <lombok.version>1.18.22</lombok.version>

        <maven.plugin.version>2.7.8</maven.plugin.version>
    </properties>

    <!-- 依赖声明 -->
    <dependencyManagement>
        <dependencies>
            <!-- SpringCloud 微服务 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- SpringCloud Alibaba 微服务 -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- SpringBoot 依赖配置 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--文档插件-->
            <dependency>
                <groupId>com.github.xiaoymin</groupId>
                <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
                <version>${knife4j.version}</version>
            </dependency>

            <!-- hutool工具 -->
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>${hutool.version}</version>
            </dependency>

            <!-- lombok -->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- SpringBoot Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--SpringBoot Actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--请求参数校验-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
View Code
EvaluationProcess 接口
package org.example.process;

public interface EvaluationProcess {

    void process(Object object);

    boolean supports(Integer type);

}
View Code

ConsultingEvaluationProcess 类

package org.example.process.impl;

import org.example.process.EvaluationProcess;
import org.springframework.stereotype.Service;

@Service
public class ConsultingEvaluationProcess implements EvaluationProcess {

    @Override
    public void process(Object object) {
        System.out.println("ConsultingEvaluationProcess");
    }

    @Override
    public boolean supports(Integer type) {
        return type == 1;
    }
}
View Code
DiagnoseEvaluationProcess类
package org.example.process.impl;

import org.example.process.EvaluationProcess;
import org.springframework.stereotype.Service;

@Service
public class DiagnoseEvaluationProcess implements EvaluationProcess {
    @Override
    public void process(Object object) {
        System.out.println("DiagnoseEvaluationProcess");
    }

    @Override
    public boolean supports(Integer type) {
        return type == 2;
    }
}
View Code
EvaluationService  业务类
package org.example.process;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class EvaluationService {

    //集合注入
    @Autowired
    private List<EvaluationProcess> evaluationProcesses;

    private Optional<EvaluationProcess> getProcess(Integer type) {
        if (this.evaluationProcesses != null) {
            return this.evaluationProcesses.stream().filter(a -> a.supports(type)).findFirst();
        }
        return Optional.empty();
    }

    public void save() {
        getProcess(1).ifPresent(origin -> origin.process(null));
    }
}
View Code
ScanConfig 配置
package org.example.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("org.example.process")
public class ScanConfig {


}
View Code

测试类  Test

package org.example.process;

import org.example.config.ScanConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test {

    public static void main(String[] args) {
        //纯注解
        ApplicationContext c1 = new AnnotationConfigApplicationContext(ScanConfig.class);

        EvaluationService bean = c1.getBean(EvaluationService.class);
        bean.save();
    }
}
View Code

源码分析:

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#createBean(java.lang.String, org.springframework.beans.factory.support.RootBeanDefinition, java.lang.Object[])
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#postProcessProperties
org.springframework.beans.factory.annotation.InjectionMetadata#inject
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement#inject
org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver#getLazyResolutionProxyIfNecessary
org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveMultipleBeans

 

标签:process,Spring,List,springframework,spring,源码,org,import,public
From: https://www.cnblogs.com/smileblogs/p/18193993

相关文章

  • 数组和List互转
    数组转List数组转List,使用JDK中java.util.Arrays工具类的asList方法。importjava.util.Arrays;importjava.util.List;publicclassMain{publicstaticvoidmain(String[]args){String[]strs={"aaa","bbb","ccc"};List......
  • 基于Java网络书店商城设计实现(源码+lw+部署文档+讲解等)
    系统介绍:随着科学技术的飞速发展,各行各业都在努力与现代先进技术接轨,通过科技手段提高自身的优势;对于网络书店商城当然也不能排除在外,随着网络技术的不断成熟,带动了网络书店商城,它彻底改变了过去传统的管理方式,不仅使服务管理难度变低了,还提升了管理的灵活性。这种个性化的平台特......
  • jwt揭秘(含源码示例和视频)
    jwt揭秘摘自:https://www.cnblogs.com/wupeiqi/p/11854573.html武沛齐博客JSONWebTokens,是一种开发的行业标准RFC7519,用于安全的表示双方之间的声明。目前,jwt广泛应用在系统的用户认证方面,特别是现在前后端分离项目。1.jwt认证流程在项目开发中,一般会按照上图所示......
  • 【java】【集合类】ArrayList扩容机制
    扩容规则1.ArrayList()无参构造扩容2.ArrayList(intinitialCapacity)扩容3.publicArrayList(Collection<?extendsE>c)扩容4.add(Objecto)扩容5.addAll(Collectionc)扩容要注意的是,以下所有代码中用反射方式来更直观地反映ArrayList的扩容特征,但从JDK9由于模......
  • 【源码】蚁群算法TSP问题可视化
    ACO.Visualization项目本项目演示蚁群算法求解旅行商问题的可视化过程,包括路径上的信息素浓度、蚁群的运动过程等。项目相关的代码:https://github.com/anycad/ACO.Visualization注:本项目基于.NET8开发,需要安装VS2022最新版本。运行效果:https://www.bilibili.com/video/BV1Bf42......
  • 后端跨域cookie问题与spring-session-data-redis
    背景1、后端统一接入了公司内部登录系统,登录后cookie信息在域名:test.net.cn下。Set-Cookie:SESSION=09a2f617-66a0-4e02-b99f-130d83900321;Domain=test.net.cn;Path=/;HttpOnly;SameSite=Lax2、当我们的系统接入到统一登录系统后,若访问域名为a.test.net.cn,则不会出现问题,因......
  • repeater中嵌套放入RadioButtonList
    <asp:RepeaterID="rptList"runat="server"onitemdatabound="rptList_ItemDataBound"><HeaderTemplate><tablewidth="100%"border="0"cellspacing="0"cellpadding="0"......
  • SpringBoot SpringCloud Spring Cloud Alibaba 版本对应关系
    最近公司的项目扫描出一些安全漏洞,于是让我给项目中的依赖升下级。有部分涉及到SpringBoot和SpringCloud,因此要考虑到兼容性,特此记录下查询各版本之间对应关系的方法。靠谱的方法还是要从官网得到,参考文章:工具篇--SpringBoot与SpringCloud的版本对应关系&SpringBoot与JDK对应关系......
  • 【Elasticsearch】系统已经配置了JAVA_HOME,ElasticSearch源码还是操作gradle失败(JAVA_
    先看下报错内容吧,如下:FAILURE:Buildcompletedwith2failures.1:Taskfailedwithanexception.-----------*Where:Buildfile'/Users/liubolun/IdeaProjects/elasticsearch/benchmarks/build.gradle'line:20*Whatwentwrong:Aproblemoccurredevalu......
  • springboot国际化的使用(一_验证OK
    https://blog.csdn.net/weixin_39960529/article/details/86571962 步骤:1.首先建立一个meven项目2.在pom.xml中加如下依赖:<dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-thymeleaf</artifactI......