Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何找到spring初始化的bean序列_Java_Spring_Hibernate_Autowired_Applicationcontext - Fatal编程技术网

Java 如何找到spring初始化的bean序列

Java 如何找到spring初始化的bean序列,java,spring,hibernate,autowired,applicationcontext,Java,Spring,Hibernate,Autowired,Applicationcontext,我的ApplicationContext包含自动连接的bean以及在application-Context.xml文件中配置的bean。我想知道spring初始化bean的顺序 我想知道这一点是因为: (我知道这是stackoveflow中的一个已知且流行的问题..但无法找到解决方案!!) 我已经在application-Context.xml中创建了SessionBean。现在尝试在DaoImpl文件中自动连接这个bean。sessionBean在那里显示为null。可能它的sessionFa

我的ApplicationContext包含自动连接的bean以及在application-Context.xml文件中配置的bean。我想知道spring初始化bean的顺序

我想知道这一点是因为: (我知道这是stackoveflow中的一个已知且流行的问题..但无法找到解决方案!!) 我已经在application-Context.xml中创建了SessionBean。现在尝试在DaoImpl文件中自动连接这个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.

假设这是spring配置文件中定义的bean
所以spring container将尝试做的是-
1.首先,它将尝试加载,即'OutputHelper'类
2.加载类时,它将检查是否存在任何依赖项
3.如果是,它将停止主bean的生命周期,即'OutputHelper',并尝试加载依赖bean'CsvOutputGenerator'。
-如果当前bean没有任何依赖项,那么它将加载该bean并移回主bean生命周期过程。
步骤2和3将适用于配置文件中提到的所有bean。

以下是您第一个问题的答案-

<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.

假设这是spring配置文件中定义的bean
所以spring container将尝试做的是-
1.首先,它将尝试加载,即'OutputHelper'类
2.加载类时,它将检查是否存在任何依赖项
3.如果是,它将停止主bean的生命周期,即'OutputHelper',并尝试加载依赖bean'CsvOutputGenerator'。
-如果当前bean没有任何依赖项,那么它将加载该bean并移回主bean生命周期过程。
步骤2和3将适用于配置文件中提到的所有bean。

Spring自动通过查看所述Bean的所有依赖项是否都已初始化来确定Bean初始化的顺序

您的案例似乎不是在使用DaoImpl类的spring初始化bean,而是在自己创建该类的新对象


尝试创建DaoImpl类的bean并使用该bean。

Spring通过查看所述bean的所有依赖项是否都已初始化来自动确定bean初始化的顺序

您的案例似乎不是在使用DaoImpl类的spring初始化bean,而是在自己创建该类的新对象


尝试创建DaoImpl类的bean并使用该bean。

bean初始化的顺序不是问题的原因。Spring将确定是否需要一个bean来进行自动连接,并在自动连接之前初始化bean。如果您有一个自动连接的字段,它是空的,那么您可能是自己创建类的对象,而不是Spring。发布一些涉及的代码。bean初始化的顺序不是问题的原因。Spring将确定是否需要一个bean来进行自动连接,并在自动连接之前初始化bean。如果您有一个自动连接的字段,它是空的,那么您可能是自己创建类的对象,而不是Spring。发布一些涉及到的代码。