Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring boot - application start twice..?

I am new to spring boot and my application has Servlet with eh-cache modules.

@EnableCaching 
@SpringBootApplication 
@ServletComponentScan
public class Application extends SpringBootServletInitializer {

    private static final Logger log = LoggerFactory.getLogger(Application.class);
    public Application() {
        log.info("------------------------------");
        log.info("  Welcome to Application");
        log.info("------------------------------");
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

using this, I was trying to start the application, but it starts twice..?

        12:11:36,020 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@291e5d10 - Registering current configuration as safe fallback point
    **2018-03-28_12:11:36.204 com.netapp.prj.Application - ------------------------------
    2018-03-28_12:11:36.226 com.ct.prj.Application -   Welcome to Application
    2018-03-28_12:11:36.226 com.ct.prj.Application - ------------------------------**

      .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v2.0.0.RELEASE)

    2018-03-28_12:11:40.371 com.ct.prj.Application - Starting Application on NBC02P05-00063 with PID 904 (H:\projects\prj\prj\target\prj\WEB-INF\classes started by   in C:\Balasundaram\Development\eclipse-jee-oxygen-R-win32-x86_64)
    2018-03-28_12:12:00.103 org.mongodb.driver.cluster - Discovered cluster type of STANDALONE
    2018-03-28_12:12:03.505 o.s.b.w.s.ServletRegistrationBean - Servlet com.ct.prj.config.JerseyConfig mapped to [/services/*]
    2018-03-28_12:12:03.508 o.s.b.w.s.ServletRegistrationBean - Servlet dispatcherServlet mapped to [/]
    2018-03-28_12:12:03.509 o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'characterEncodingFilter' to: [/*]
    2018-03-28_12:12:03.509 o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'errorPageFilter' to: [/*]
    2018-03-28_12:12:03.509 o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    2018-03-28_12:12:03.510 o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'httpPutFormContentFilter' to: [/*]
    2018-03-28_12:12:03.510 o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'requestContextFilter' to: [/*]
    2018-03-28_12:12:03.511 o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'httpTraceFilter' to: [/*]
    2018-03-28_12:12:03.511 o.s.b.w.s.FilterRegistrationBean - Mapping filter: 'webMvcMetricsFilter' to: [/*]
    **2018-03-28_12:12:03.558 com.ct.prj.Application - ------------------------------
    2018-03-28_12:12:03.559 com.ct.prj.Application -   **Welcome to Application**
    2018-03-28_12:12:03.559 com.ct.prj.Application - ------------------------------**
    2018-03-28_12:12:06.952 o.s.b.a.w.s.WelcomePageHandlerMapping - Adding welcome page: class path resource [static/index.html]

Please correct me where i am doing mistake..?

like image 714
Boss Avatar asked Dec 19 '22 00:12

Boss


1 Answers

The output is coming twice but the application is actually started only once.

There are actually 2 instances going to be created and that is actually due to the @SpringBootApplication annotation. Or more precisely the @Configuration annotation.

When Spring detects a @Configuration it will create a special proxy for this class. As this is a class based proxy a new instance will get created to be able to proxy calls to all @Bean methods (so that you only get a single instance of the bean etc.). This proxy will wrap the actual instance.

However the main class will only run once and the proxied instance will be used to get the @Bean methods from.

like image 185
M. Deinum Avatar answered Jan 04 '23 06:01

M. Deinum