Spring Boot 库项目未从 /conf 文件夹中选择外部文件配置

Posted

技术标签:

【中文标题】Spring Boot 库项目未从 /conf 文件夹中选择外部文件配置【英文标题】:Spring Boot Library project not picking External file configuration from /conf folder 【发布时间】:2020-02-23 14:36:39 【问题描述】:

正在将spring应用程序迁移到spring boot。一些配置需要设置为外部。这些文件放在jar文件所在的/conf文件夹中。我有多个项目,一个是父项目,其他是库项目。父项目和库项目无法选择 /conf 文件夹配置文件。

迁移后的当前项目结构

父项目主类配置

@SpringBootApplication(exclude =  DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
        org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class )
@ComponentScan(basePackages =  "com.data.*" )
@Profile(ContextProfileNames.SERVICE)
@ImportResource(locations = 
        "/conf/spring/service-config.xml",
        "/conf/spring/datasource-config.xml"

)
public class ServiceMain 

    public static void main(String[] args) 

        ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(ServiceMain.class)
                .properties("spring.config.name:application.properties",
                        "spring.config.location=/conf/application.properties")
                .build()
                .run(args);
        for (String name : applicationContext.getBeanDefinitionNames()) 

        

    

库项目配置

@Configuration
public class ConfigurationFactory

    public static final String extConfPath="/conf";

    public static final String REQ_CONF = extConfPath+"/Configuration.xml";
    public static final String FILTER_XML_CONF = extConfPath+"/DocFilter.xml";

错误日志

Caused by: java.io.FileNotFoundException: \conf\Configuration.xml (The system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
    at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
    at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:221)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:276)
    at 

编辑 1

父项目主类配置

@SpringBootApplication(exclude =  DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
        org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class )
@ComponentScan(basePackages =  "com.data.*" )
@Profile(ContextProfileNames.SERVICE)
@ImportResource(locations = 
        "conf/spring/service-config.xml",
        "conf/spring/datasource-config.xml"

)
public class ServiceMain 

    public static void main(String[] args) 

        ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(ServiceMain.class)
                .properties("spring.config.name:application.properties",
                        "spring.config.location=conf/application.properties")
                .build()
                .run(args);
        for (String name : applicationContext.getBeanDefinitionNames()) 

        

    

库项目配置

@Configuration
public class ConfigurationFactory

    public static final String extConfPath="conf";

    public static final String REQ_CONF = extConfPath+"/Configuration.xml";
    public static final String FILTER_XML_CONF = extConfPath+"/DocFilter.xml";

现在又出现一个错误

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.7.RELEASE)

00:26:01.203 [main] DEBUG org.springframework.boot.SpringApplication - Loading source class com.data.services.api.ServiceMain
00:26:01.266 [main] DEBUG o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2c42e416
00:26:01.297 [main] DEBUG o.s.b.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
00:26:01.339 [main] DEBUG o.s.b.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory'
00:26:01.631 [main] DEBUG o.s.b.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
00:26:01.631 [main] DEBUG o.s.b.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
00:26:01.631 [main] DEBUG o.s.b.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
00:26:01.631 [main] DEBUG o.s.b.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
00:26:01.631 [main] DEBUG o.s.b.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor'
00:26:01.647 [main] DEBUG o.s.ui.context.support.UiApplicationContextUtils - Unable to locate ThemeSource with name 'themeSource': using default [org.springframework.ui.context.support.ResourceBundleThemeSource@d935005]
00:26:01.647 [main] WARN  o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

00:26:01.662 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:743)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:390)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
    at com.ge.hcit.xer.app.services.api.XERServiceMain.main(XERServiceMain.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:543)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:203)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153)
    ... 12 common frames omitted
[WARNING] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:543)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:743)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:390)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
    at com.ge.hcit.xer.app.services.api.XERServiceMain.main(XERServiceMain.java:29)
    ... 6 more
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:203)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153)
    ... 12 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

编辑 2

如何使用 spring.config.location 而不是使用以下注释硬编码导入资源和 application.pproperties 中的值。如何在 java 端阅读 spring.config.location p>

java -jar myproject.jar --spring.config.location=D:/Springboot/conf

我该如何解决这个问题?

【问题讨论】:

第二次编辑请查看***.com/a/25862357/4929038 【参考方案1】:

不要在路径的开头使用斜杠,它将指向驱动器的根目录(Windows 和 Linux 上都如此)。没有斜杠 (conf/application.properties) 的路径应该可以工作,因为它将是一个相对路径。

【讨论】:

我已经改变了,基于你的 cmets.now 得到另一个错误。 快速谷歌搜索链接到this SO 主题。希望对您有所帮助。 无法理解这一点。我可以加载 application.property 文件,但无法使用 @ImportantResource() 导入外部 xml 文件

以上是关于Spring Boot 库项目未从 /conf 文件夹中选择外部文件配置的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 应用程序未从 Spring Cloud Config Server 读取属性值

Spring Boot、Spring Cloud AWS 和 AWS SQS 未从队列中读取

未从 application.properties 中提取 Spring Boot JOOQ sql 方言

Spring Boot 未从 application.properties 加载用户名

前端 (HTML) 未从 Microsoft Azure 中的 mysql 数据库获取 Spring Boot 应用程序中的数据

Spring Boot - 自动生成接口文档