@Component @Scope("singleton")公共类BootStrapper实现ApplicationListener <ContextStartedEvent> {

Cha*_*apu 8 spring web-applications

我正在尝试对我的webapp进行一次初始化.我需要ApplicationListener类的单例,所以我将范围设置为Singleton,但它创建了多个实例.此BootStrapper未在任何其他xml配置文件中定义.我知道默认范围是单例,但必须添加@Scope("singleton"),因为它不是单例.即使使用此注释,它仍会创建多个实例.这是我的ApplicationListener.

@Component
@Scope("singleton")
public class BootStrapper implements ApplicationListener<ContextRefreshedEvent> {
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

Ral*_*lph 5

要在初始化bean之后调用一个回调,请使用@PostConstruct.

@Component
public class BootStrapper() {

     @PostConstruct
     public void doSomething() {
          System.out.println("I am initalized!");
     }
}
Run Code Online (Sandbox Code Playgroud)