spring - @PostConstruct 和在配置类中使用 new 创建的 bean

标签 spring spring-boot testing postconstruct

我在 spring boot 中有一个服务 @PostConstruct方法。在某些集成测试中,不应执行此方法(在这些集成测试中根本不使用该服务)。所以我想我可以用 new 创建 bean在此测试中加载的配置类中 (@ContextConfiguration)。

然而,这并没有像我想的那样起作用。 @PostConstruct无论如何都会被调用,甚至两次,如下所示(是的,这就是全部代码):

申请

@SpringBootApplication
public class DemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

服务

@Service
public class MyService {

  @PostConstruct
  public void startup() {
    System.out.println("@PostConstruct - " + this);
  }
}

测试

@RunWith(SpringRunner.class)
@SpringBootTest(
    webEnvironment = WebEnvironment.RANDOM_PORT,
    classes = {DemoApplication.class})
@ContextConfiguration(classes = TestConfiguration.class)
public class DemoApplicationTests {

  @Test
  public void test() {
    System.out.println("Test");
  }

  @Configuration
  static class TestConfiguration {

    @Bean
    public MyService xxx() {
      MyService myService = new MyService();
      System.out.println("@Bean - " + myService);
      return myService;
    }

  }
}

如果执行测试,将打印以下输出:

 :: Spring  Boot ::        (v2.1.1.RELEASE)
...
2018-11-30  20:34:28.422  INFO 16916 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-11-30  20:34:28.422  INFO 16916 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1573 ms
@PostConstruct  - com.example.demo.MyService@41c89d2f
@Bean - com.example.demo.MyService@2516fc68
@PostConstruct  - com.example.demo.MyService@2516fc68
2018-11-30  20:34:28.838  INFO 16916 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2018-11-30  20:34:29.086  INFO 16916 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 62040 (http) with context path ''
2018-11-30  20:34:29.090  INFO 16916 --- [           main] com.example.demo.DemoApplicationTests    : Started DemoApplicationTests in 2.536 seconds (JVM running for 4.187)
Test
2018-11-30  20:34:29.235  INFO 16916 --- [       Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

有人能给我解释一下吗?

  • 为什么是第一个 @PostConstruct
  • 为什么是其次@PostConstruct要求使用 new 构建的 bean .为什么这个 bean 由 spring 管理?
  • 我怎样才能达到我的需要?

编辑:
我试图返回一个用 Mockito.mock(...) 创建的 bean而不是用 new 创建它.这在某种意义上有助于第二个 @PostConstruct没有被执行。所以问题仍然存在:为什么是第一个?以及如何摆脱它?

最佳答案

我将解释您在所有情况下看到的行为,以便您了解发生了什么。

第一个 PostConstruct 被调用是因为您正在使用 SpringRunner@SpringBootTest 运行测试,这是扫描您的类路径并注册 MyService作为 Bean,因为它用 @Service 注释。

第二个 PostConstruct 被调用是因为即使您正在更新 MyService,您也是在一个用 @Bean 注释的方法中这样做的,它在 Spring 上下文中注册了 bean,因此它参与了生命周期就像任何其他 bean 一样(包括调用其 @PostConstruct@PreDestroy 方法)。

如果您不想在 SpringBootTests 中使用 MyService 的真实实例,您可以使用 @MockBean。在您的 SpringBootTests 中,您可能希望 MockBean 而不是 Mock,因为它会在您的 Spring 上下文中模拟 bean。

关于spring - @PostConstruct 和在配置类中使用 new 创建的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53564296/

相关文章:

java - 将 SOAP header 添加到 spring-ws 2.2.0 端点响应

spring - Spring Batch 的 Mongo 存储库?

java - 如何正确使用MapStruct @Mapping和@Mappings?

testing - 在执行 TDD 时发现其他对象

java - Spring如何与Hibernate集成

java - 多模块maven VS 微服务架构

spring - STS Spring Boot 仪表板无法识别成功启动

ruby-on-rails - 简单的 cucumber 测试问题

ruby-on-rails - ActiveRecord 在测试中的奇怪行为

java - Spring @Valid注解