gpt4 book ai didi

java - 有没有等同于@Startup的spring?

转载 作者:行者123 更新时间:2023-11-28 22:08:32 24 4
gpt4 key购买 nike

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

是否有 spring 等价物,或者你将如何在 spring 中做到这一点?

提前致谢!

最佳答案

不确定这是否是您要的。我总是在我的 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() {

}
}

添加一个示例,说明如何编写单元测试以试验 bean 在 Spring 上下文中的行为。

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

关于java - 有没有等同于@Startup的spring?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21600153/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com