Home > database >  Why did the PostConstruct execute twice in Spring
Why did the PostConstruct execute twice in Spring

Time:10-30

I'm using Spring to develop a web application. I just used the mechanism @PostConstruct and @Bean to invoke a function while starting up.

public class MyCache<T> {
    @PostConstruct
    public void init() {
        System.out.println("inittttttttttt");
        // something
    }
}

@Configuration
@EnableScheduling
public class AppConfig {
    @Bean
    public MyCache<MyData> myCache() {
        return new MyCache<MyData>();
    }
}

public class TextFilter {
    private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    private MyCache<MyData> cache;

    public TextFilter() {
        this.context.scan("com.sensitive_words.utils");
        this.context.refresh();
        this.cache = this.context.getBean(MyCache.class);
    }

    public String filter(String originalText) {
        return this.cache.get().filter(originalText);
    }
}

As you see, I created a Bean and use the Bean in the class TextFilter.

However, I found that the function init() is executed twice. Here is the log:

2022-10-30 13:53:38.837  INFO 106994 --- [           main] y.c.s.SensitiveWordsApplication          : Starting SensitiveWordsApplication using Java 11.0.16 on yves-Inspiron-5488 with PID 106994 (/home/yves/java_ws/sensitive_words/target/classes started by yves in /home/yves/java_ws/sensitive_words)
2022-10-30 13:53:38.839  INFO 106994 --- [           main] y.c.s.SensitiveWordsApplication          : No active profile set, falling back to 1 default profile: "default"
2022-10-30 13:53:39.545  INFO 106994 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8090 (http)
2022-10-30 13:53:39.559  INFO 106994 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-10-30 13:53:39.560  INFO 106994 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.68]
2022-10-30 13:53:39.654  INFO 106994 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-10-30 13:53:39.654  INFO 106994 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 763 ms
inittttttttttt
2022-10-30 13:53:39.769  INFO 106994 --- [           main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
inittttttttttt
2022-10-30 13:53:40.080  INFO 106994 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8090 (http) with context path ''
2022-10-30 13:53:40.096  INFO 106994 --- [           main] y.c.s.SensitiveWordsApplication          : Started SensitiveWordsApplication in 1.64 seconds (JVM running for 2.341)

Could you help me?

CodePudding user response:

As I see you are setting up two different application contexts of spring

  1. SensitiveWordsApplication does it for you
  2. AnnotationConfigApplicationContext context you created manually in code.

This seems to be the reason, singleton bean is created one per container by default.

  • Related