gpt4 book ai didi

spring - 多个 EmbeddedServletContainerFactory beans

转载 作者:行者123 更新时间:2023-11-28 22:58:59 27 4
gpt4 key购买 nike

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

即创建了一个类似于下面的类:现在当我启动我的 spring boot(building jar not war)时,我收到以下错误

无法启动嵌入式容器;嵌套异常是 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;
}

}

最佳答案

好的,这就是我为解决问题所做的工作。我在我的类(class)周围放置了一堆系统输出,想看看什么叫什么,什么不叫。我看到我的客户 tomcat 类没有被调用。然后我删除了“@EnableAutoConfiguration”然后我看到打印了 TomcatEmbeded bean 的系统输出并且 tomcat 端口设置为 8888。我必须承认我不知道 @EnableAutoConfiguration 到底在做什么但是我尝试了几次这些步骤项目清理后,maven 清理,maven 重建以确保我没有犯任何错误。这是我在没有“@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);


}





}

关于spring - 多个 EmbeddedServletContainerFactory beans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22393242/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com