因此,我编写了自己的SpringBootStarter,它应该在SpringBoot应用程序的嵌入式tomcat中启用JNDI查找。
我的示例SpringBoot应用程序具有自定义SpringBootStarter的依赖关系,而自定义SpringBootStarter又依赖于SpringBoot starter。如果我在示例SpringBoot应用程序中创建了一个配置类,如下面所示,那么一切都可以很好地工作:
@Configuration
public class SampleSpringBootAppConfig {
@Bean
public TomcatServletWebServerFactory tomcatFactory() {
return new TomcatServletWebServerFactory() {
@Override
protected TomcatWebServer getTomcatWebServer(org.apache.catalina.startup.Tomcat tomcat) {
System.out.println("CONFIGURING CUSTOM TOMCAT WEB SERVER FACTORY ");
tomcat.enableNaming();
return super.getTomcatWebServer(tomcat);
}
@Override
protected void postProcessContext(Context context) {
ContextResource resource = new ContextResource();
resource.setName("myDataSource");
resource.setType(DataSource.class.getName());
resource.setProperty("driverClassName", "org.postgresql.Driver");
resource.setProperty("url", "jdbc:postgresql://localhost:5432/postgres");
resource.setProperty("username", "postgres");
resource.setProperty("password", "postgres");
context.getNamingResources()
.addResource(resource);
}
};
}因为SpringBoot找到了一个自定义Bean,所以不会有一个自动配置的默认Bean/它将被覆盖,并且JNDI将被成功启用。
但是,一旦我将此Bean配置解压缩到我的自定义SpringBoot启动程序的自动配置模块中,在尝试启动示例SpringBoot应用程序时将引发以下异常:
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to multiple ServletWebServerFactory beans : tomcatServletWebServerFactory,tomcatFactory我认为这是因为SpringBoot没有找到自定义Bean,因此创建了一个自动配置的默认Bean,它也不会被覆盖。现在将有两个ServletWebServerFactory bean,默认的和我的自动配置模块中的一个。
我到目前为止所做的尝试(没有结果):
用@Primary
注释自定义Bean
是否有任何方法使SpringBoot不初始化自动配置的默认bean,或者对此有任何其他可能的解决方案?
发布于 2019-11-13 08:25:46
通过排除负责任的AutoConfiguration类,我自己解决了这个问题:
@SpringBootApplication ( exclude = ServletWebServerFactoryAutoConfiguration.class)发布于 2019-12-09 05:30:15
试试这个@AutoConfigureBefore(ServletWebServerFactoryAutoConfiguration.class)
https://stackoverflow.com/questions/58821412
复制相似问题