2016-12-17 85 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。