2

我正在开发一个需要运行 TCP 服务器的应用程序,这是它首先要做的事情之一。这目前是通过 Spring 配置类启动的:

@PostConstruct
public void initTCPServer() {
    // Gets the port number and other values from the database...
    // Note: This uses dependency injection of the Data Access Objects (DAOs).
}

它在使用现有的预填充数据库时工作正常,但在尝试编写集成测试时遇到问题:数据需要预先填充,但@PostConstruct如果它在 JUnit@Before方法中,则在数据填充之前触发,并且使用 SpringJUnit4ClassRunner 的@TestExecutionListeners.

我现在能想到的唯一解决方案是使用 a 初始化测试配置类中的数据@PostConstruct并确保首先加载它 - 但这似乎有点脏。编辑:刚刚尝试过,但由于 BeanCurrentlyInCreationException 而失败 - 看起来 EntityManagerFactory 的依赖注入尚未完成。

有没有更优雅的方式(例如我应该在其他地方运行 TCP 服务器,即不由 Spring 管理)?

4

0 回答 0