我必须将企业应用程序转换为spring。到目前为止,我已经完成了所有工作。但是我仍然需要在我的两个bean上替换@Startup注释。
是否有弹簧等效物?或者你会在春天怎么做?
提前致谢!
答案 0 :(得分:3)
不确定这是否是您要求的。我总是在我的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() {
}
}
添加一个示例,说明如何编写单元测试以在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.
}
}