0
votes

I have a working spring mvc created with spring boot and embeded tomcat. Now I would like to change some default setting of the tomcat i.e. port setSessionTimeout etc. from the spring boot readme (http://projects.spring.io/spring-boot/docs/spring-boot/README.html) I have copied the example

i.e. created a class that looks like the one below: now when i start my spring boot (building jar not war) i am getting the following error

Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to multiple EmbeddedServletContainerFactory beans : servletContainer,tomcatEmbeddedServletContainerFactory

I understand that there are two EmbeddedServletContainerFactory's created but why is that I thought that whatever bean i declare of type EmbeddedServletContainerFactory it should override the existing default one. If i rename the EmbeddedServletContainerFactory from servletContainer() to tomcatEmbeddedServletContainerFactory() the error is gone however the port i set is not 8888 anymore but 8080 the default one. And it seems that it is working but not accepting the my customer settings i.e. port 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

1 Answers

1
votes

ok here is what i did to solve my problem. I have places bunch of sysouts around my classes and wanted to see what is called and what not. I saw that my customer tomcat class was not called. Then I removed the "@EnableAutoConfiguration" and then i saw that the sysouts from the TomcatEmbeded bean got printed and the tomcat port got set to 8888. I must confess i do not know what exactly @EnableAutoConfiguration is doing but i tried those steps several times after project clean, maven clean, maven rebuild to make sure that i did not made any mistake. Here is my Application class that i use without the "@EnableAutoConfiguration" annotation. Hope this helps someone.

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


    }





}