spring boot - 应用程序启动两次..?

时间:2018-03-28 06:48:48

标签: java spring spring-boot spring-boot-test

我是春季启动的新手,我的应用程序有Servlet和eh-cache模块。

@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]

请纠正我在哪里犯错误。?

4 个答案:

答案 0 :(得分:3)

输出结果是两次,但应用程序实际上只启动了一次。

实际上会创建2个实例,这实际上是由@SpringBootApplication注释引起的。或者更准确地说是@Configuration注释。

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

但是main类只运行一次,代理实例将用于获取@Bean方法。

答案 1 :(得分:1)

只有班级Application被实例化了两次。该应用程序启动了一次。

答案 2 :(得分:0)

我观察到的情况与我为devtools添加依赖项相同

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

devtools自动刷新上下文。 希望对您有所帮助。

答案 3 :(得分:0)

我必须删除 spring-boot-devtools 的依赖关系,然后才能成功启动应用程序。