是否有相当于@Startup 的弹簧?

Ken*_*ans 3 java tomcat quartz-scheduler

我必须将企业应用程序转换为 spring。到目前为止,我已经一切正常。但是我仍然需要替换我的 2 个 bean 上的 @Startup 注释。

是否有弹簧等价物,或者您将如何在春季做到这一点?

提前致谢!

aks*_*mit 6

不确定这是否是您所要求的。我总是在我的 Spring-beans 中使用 @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() {

    }
}
Run Code Online (Sandbox Code Playgroud)

添加一个示例,说明如何编写单元测试来试验 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.
    }
}
Run Code Online (Sandbox Code Playgroud)