2016-12-17 86 views
0

我的ApplicationContext被自动装配,以及那些在应用context.xml文件配置豆豆豆初始化的序列。我想知道Spring的bean初始化顺序。如何找到弹簧

我想知道这是因为: (我知道这是在stackoveflow..but一个已知的和流行的问题不能得到解决!!) 我已经在应用程序的context.xml创建会话Bean。现在要在DaoImpl文件中自动装入这个bean。 sessionBean在那里显示为空。可能是它的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. 

请帮助,因为我打!

在此先感谢。

+0

bean初始化的顺序不是问题的原因。 Spring将确定是否需要自动装配的bean,并在之前初始化bean。如果你有一个autowired字段,它是null,你可能自己创建一个类的对象,而不是Spring。发布一些涉及的代码。 – dunni

回答

1

这里是你的第一个question-的答案

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

春季汽车通过看,如果说bean的所有依赖初始化与否决定豆初始化序列。

你的情况我好像你不使用DaoImpl类的初始化春豆而你自己创建类的新对象。

尝试创建DaoImpl类的豆和使用bean。