多个EmbeddedServletContainerFactory bean

时间:2014-03-13 23:33:00

标签: spring tomcat frameworks javabeans

我有一个使用spring boot和嵌入式tomcat创建的工作spring mvc。 现在我想从spring boot自述文件中更改tomcat的一些默认设置,即port setSessionTimeout等 (http://projects.spring.io/spring-boot/docs/spring-boot/README.html)我复制了示例

即。创建了一个类似下面的类: 现在,当我开始我的春季启动(建立jar不战争)我收到以下错误

无法启动嵌入式容器;嵌套异常是org.springframework.context.ApplicationContextException:由于多个EmbeddedServletContainerFactory bean,无法启动EmbeddedWebApplicationContext:servletContainer,tomcatEmbeddedServletContainerFactory

我知道有两个EmbeddedServletContainerFactory被创建,但为什么我认为无论我声明类型为EmbeddedServletContainerFactory的bean应该覆盖现有的默认值。如果我将EmbeddedServletContainerFactory从servletContainer()重命名为tomcatEmbeddedServletContainerFactory(),则错误消失,但我设置的端口不再是8888,而是默认值8080。它似乎工作但不接受我的客户设置,即端口8888

import java.util.concurrent.TimeUnit;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TomcatEmbeded {

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
    factory.setPort(8888);
    factory.setSessionTimeout(5, TimeUnit.MINUTES);
    //factory.addErrorPages(new ErrorPage(HttpStatus.404, "/notfound.html");

    return factory;
}

}

1 个答案:

答案 0 :(得分:1)

好的,这就是我为解决问题所做的工作。我在我的课程周围放置了大量的sysout,并希望看到什么叫做什么叫做什么。我看到我的客户tomcat类没有被调用。然后我删除了" @ EnableAutoConfiguration"然后我看到TomcatEmbeded bean的sysouts被打印,tomcat端口被设置为8888.我必须承认我不知道究竟是什么@EnableAutoConfiguration正在做什么但我在项目清理之后尝试了几次这些步骤,maven clean,maven重建,以确保我没有犯任何错误。这是我使用的Application类,没有" @ EnableAutoConfiguration"注解。希望这有助于某人。

@ComponentScan(basePackages = "org.syncServer")
@Configuration
@EnableWebSocketMessageBroker
public class Application extends SpringBootServletInitializer {


    @Bean
    public ServletRegistrationBean dispatcherRegistration() {

        System.out.println("SERVLET REGISTRATION");
        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet());

        System.out.println("SERVLET REGISTERED NAME is: " + registration.getServletName().toString()); 
        //registration.setLoadOnStartup(1);    
        registration.addUrlMappings("/");


            return registration;
    }

    @Bean(name = DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
    public DispatcherServlet dispatcherServlet()
    {
        System.out.println("DISPATCHER INIT");

//      DispatcherServlet dispatcherServlet = new  DispatcherServlet();
//      System.out.println("SERVLET REGISTERED NAME is: " + dispatcherServlet.getServletName().toString());
//      return dispatcherServlet;
        return new DispatcherServlet();

    }

    @Bean
    protected ServletContextListener listener() {
        return new ServletContextListener() {
            @Override
            public void contextInitialized(ServletContextEvent sce) {
                System.out.println("SERVLET CONTEXT initialized");
                logger.info("SERVLET CONTEXT initialized");
            }

            @Override
            public void contextDestroyed(ServletContextEvent sce) {
                System.out.println("SERVLET CONTEXT initialized");
                logger.info("SERVLET CONTEXT destroyed");
            }
        };
    }



    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

        SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class).child(TomcatEmbeded.class, MVCConfig.class, PersistenceConfig.class);
        return builder;
        //return application.sources(Application.class);
    }


    public static void main(String[] args) {



//        ApplicationContext ctx = new AnnotationConfigApplicationContext(Application.class);

        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println("PRINTING the beans provided by Spring Boot:");
        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }




        System.out.println("PRINTING SELECTED VIEW RESOLVERS ORDER:");
        Map<String, ViewResolver> resolvers = ctx.getBeansOfType(org.springframework.web.servlet.ViewResolver.class);

        for (Entry<String, ViewResolver> entry : resolvers.entrySet()) {
            System.out.println("key=" + entry.getKey() + ", value=" + entry.getValue());

        }

//        InternalResourceViewResolver ire = ctx.getBean(InternalResourceViewResolver.class);
//        int order = ire.getOrder();
//        System.out.println("InternalResourceViewResolver Order is:" + order);

//        ThymeleafViewResolver tre = ctx.getBean(ThymeleafViewResolver.class);
//        int torder = tre.getOrder();
//        System.out.println("ThymeleafViewResolver Order is:" + torder);


    }





}