简体   繁体   English

Spring 基于其他bean列表的bean创建

[英]Spring Bean creation based on a list of other beans

Anyone can explain how Spring defines the bean creation mechanism when create a bean which depends on a list of other beans?任何人都可以解释 Spring 在创建依赖于其他 bean 列表的 bean 时如何定义 bean 创建机制? It would be good to show the part of Spring specification on how it's defined.最好展示 Spring 规范中关于它是如何定义的部分。

Code like:代码如:

public interface Test {
}

@Service
public class TestImpl1 implements Test{
}

@Service
public class TestImpl2 implements Test{
}

public class TestContainer {
    List<Test> testList;
    TestContainer() {
        testList = new ArrayList<>();
    }

    public void addTest(Test test) {
        testList.add(test);
    }
}

then然后

@Bean
public TestContainer testContainer(List<Test> testList) {
    TestContainer testContainer = new TestContainer();
    for (Test test : testList) {
        testContainer.addTest(test);
    }
    return testContainer;
}

Question is really: when creating bean for TestContainer, how does Spring figure out what should be in List testList?问题真的是:在为 TestContainer 创建 bean 时,Spring 如何确定 List testList 中应该包含什么?

This looks like what you are looking for:这看起来像您正在寻找的内容:

https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-class https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-class

You haven't specified what exactly do you want to know about bean creation but here is the minimum you should know.您还没有具体说明您想知道关于 bean 创建的具体内容,但这是您应该知道的最低要求。 By default all beans are singleton scoped (only created once during the container life-cycle and for all the subsequent request the same instance is returned).默认情况下,所有 bean 都是 singleton 范围的(在容器生命周期中只创建一次,并且对于所有后续请求,返回相同的实例)。 All singleton scoped beans are created eagerly.所有 singleton 范围的 bean 都是急切创建的。 If the singleton bean is dependent on some other beans (needs them for instantiation) then those other beans will be instantiated with it/right before it, doesn't matter whether they are singletons or not, marked as lazy or not.如果 singleton bean 依赖于其他一些 bean(需要它们进行实例化),那么那些其他 bean 将用它/在它之前实例化,不管它们是否是单例,标记为懒惰与否。

This is part of the spring documentation I think on their website.这是我认为在他们的网站上的 spring 文档的一部分。 Look for it.寻找它。 The whole spring documentation is worth to read, even if it is a long read ( https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#spring-core )整个 spring 文档都值得一读,即使是长篇阅读( https://docs.spring.io/spring/docs#spring-core/spring-framework

But mostly, spring would first read all the XML, anotations and other source of config data and get a list of beans to init.但大多数情况下,spring 将首先读取所有 XML、注释和其他配置数据源,并获取要初始化的 bean 列表。 Then it would get a list of dependencies between beans constructing kind of a tree.然后它会得到一个构建树的bean之间的依赖列表。 As the dependencies need to be initialized first, there an obvious orderining for initialization.由于需要首先初始化依赖项,因此初始化的顺序很明显。

On top, you can set your own priorities for beans so that you can get some beans initialized first or last for example.最重要的是,您可以为 bean 设置自己的优先级,以便您可以首先或最后初始化一些 bean。

For your specific case, spring will inspect the code source as well as use the Java reflection API to figure out you want a collection of interface Test implementations.对于您的特定情况,spring 将检查代码源,并使用 Java 反射 API 来确定您需要接口测试实现的集合。 So spring would look for ALL the defined bean that match and return them that not more complex than that.因此 spring 将查找所有匹配的已定义 bean,并返回不比这更复杂的它们。

It's mostly using the concepts of graph data structure where the beans become the nodes of the graph and they are resolved using topological sort.它主要使用图形数据结构的概念,其中 bean 成为图形的节点,并使用拓扑排序来解决它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM