2014-02-06 54 views
1

我必須將企業應用程序轉換爲spring。到目前爲止,我已經掌握了一切。但是我仍然需要在我的兩個bean上替換@Startup註釋。有沒有相當於@Startup的彈簧?

是否有春季等價物,或者春天你會怎麼做?

在此先感謝!

+0

'SmartLifeCycle'? – 2014-02-06 10:27:22

+0

你能解釋它的作用嗎? – chrylis

+0

@RC。你可以解釋嗎? –

回答

3

不知道這是你要求的。我總是用@PostConstruct註釋在我的春節,豆,做的東西,需要在應用程序的啓動做:

@Component 
public class SchedulerBootstrap { 

    @Autowired 
    MyRepository myRepository; 

    @Autowired 
    OpenPropertyPlaceholderConfigurer configurer; 

    @PostConstruct 
    /** 
    * This method will be called after the bean has been 
    * instantiated and all dependencies injected. 
    */ 
    public void init() { 

    } 
} 

加入你如何編寫單元測試的例子與行爲實驗在Spring環境中的bean。

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; 

@ContextConfiguration(locations = {"classpath:spring-context.xml"}) 
public class BootstrapTest extends AbstractJUnit4SpringContextTests { 

    @Autowired 
    SchedulerBootstrap schedulerBootstrap; 

    @Test 
    public void myTest() { 
     //Some code that berifies that init-method had been called. 
     //Or start unit test in debug-mode and add a breakpoint in the 
     //init-method, you will see it being called before the test is executed. 
    } 
} 
+0

我有@PostConstruct aswel並且正在考慮同樣的事情。 '@Startup'在春天可能不需要,因爲我相信,beans在啓動時已經初始化。雖然我很想確定一些確認。 –

+0

是的,我可以確認它正在工作。當然,你需要定義一個初始化bean的spring-context。請查看我添加的關於如何編寫單元測試以確認行爲的示例。 – aksamit

+0

謝謝你!猜猜我已經完成了。 –