gpt4 book ai didi

java - 如何找到spring初始化的bean的顺序

转载 作者:行者123 更新时间:2023-12-02 03:09:59 24 4
gpt4 key购买 nike

我的 ApplicationContext 具有 Autowiring 的 Bean 以及在 application-Context.xml 文件中配置的 Bean。我想知道spring初始化bean的顺序。

我想知道这一点是因为:(我知道这是 stackoveflow 中的一个已知且流行的问题..但无法找到解决方案!!)我已经在 application-Context.xml 中创建了 SessionBean。现在尝试在 DaoImpl 文件中 Autowiring 这个 bean。 sessionBean 在那里显示为 null。可能是它的 sessionFactory 在那之前还没有初始化。

*我尝试使用 @DependsOn("SessionFactory") 但失败。

所以我的问题是:

1)How to find the sequence of beans initialised by spring.
2)How do say to spring to initilise sessionfactory before initialising my DAOImpl class.

请帮助我,因为我很震惊!

提前致谢。

最佳答案

这是您第一个问题的答案-

<bean id="OutputHelper" class="com.mkyong.output.OutputHelper">
<property name="outputGenerator" >
<ref bean="CsvOutputGenerator"/>
</property>
</bean>

<bean id="CsvOutputGenerator" class="com.mkyong.output.CsvOutputGenerator">
<property name="name"value="hi"/ >
</bean>

say this is the bean defined in your spring config file
so what spring container will try to do is -
1. 1st it will try to load i.e `OutputHelper` class
2. While loading the class it will check if there is any dependency
3. if yes,It will stop life cycle of main bean i.e `OutputHelper` and try to load dependent bean 'CsvOutputGenerator'.
- If current bean does not have any dependency then it will load the bean and moved back to main bean life cycle process.

step 2 &3 will be applicable for all the bean mentioned in config file.

关于java - 如何找到spring初始化的bean的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41196628/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com