编程语言
首页 > 编程语言> > java-春季启动-应用程序启动两次..?

java-春季启动-应用程序启动两次..?

作者:互联网

我是Spring Boot的新手,我的应用程序具有带有eh-cache模块的Servlet.

@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);
    }
}

使用这个,我试图启动该应用程序,但是它启动了两次.

        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]

请纠正我在哪里做错了..?

解决方法:

输出将输出两次,但实际上该应用程序仅启动一次.

实际上将要创建2个实例,这实际上是由于@SpringBootApplication注释所致.或更准确地说是@Configuration注释.

当Spring检测到@Configuration时,它将为该类创建一个特殊的代理.由于这是一个基于类的代理,因此将创建一个新实例,以便能够代理对所有@Bean方法的调用(这样,您只能获得Bean等的一个实例).该代理将包装实际实例.

但是,主类将仅运行一次,并且代理实例将用于从中获取@Bean方法.

标签:spring-boot-test,spring-boot,spring,java
来源: https://codeday.me/bug/20191025/1927333.html