当前位置: 首页 > 知识库问答 >
问题:

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

钱言
2023-03-14

正在将Spring应用程序迁移到Spring引导。有些配置需要设置为外部。这些文件放在 /confjar文件所在的文件夹中。我有不止一个项目,一个是家长项目,其他是图书馆项目。父项目和库项目无法选择 /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。配置。位置,而不是硬编码导入资源和应用程序中的值。p使用以下注释创建属性。我怎样才能读《Spring》。配置。java端的位置

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

我如何解决这个问题?

共有1个答案

臧欣怿
2023-03-14

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

 类似资料:
  • 我在为Liferay创建portlet时遇到了困难。我有标准安装,Liferay门户位于中,插件SDK位于中。 我已经在liferay_home/plugins/portlet/my portlet中放置了新的portlet应用程序。从那里我运行,我可以看到WAR文件已成功组装并复制到。 然而,在运行的Liferay Portal instancel中,我没有看到刚刚复制到部署文件夹的portle

  • 我想通过使用“文件类”从项目文件夹中获取文件,我如何做到这一点?

  • 问题内容: 我是Java的新手。我正在尝试动态选择文件位置以保存我的项目的结果(将在我的项目开始时启动)。我研究了一些FileDialog示例,但是每个示例都允许我选择文件而不是文件夹。 谁能帮我提供一个示例(或)链接到同一示例吗? 问题答案: 您可以尝试执行以下操作(如此处所示:使用JFileChooser选择目录):

  • 问题内容: 我想使用“文件类”从项目文件夹中获取文件,我该怎么办? 问题答案: 嗯,有很多不同的方法来获取Java文件,但这是一般要点。 不要忘记,您至少需要将其包装在a 中,因为File是其中的一部分,这意味着它必须具有try-catch块。 不是要解决Ericson的问题,而是如果您使用的是实际的软件包,则除非明确使用文件的位置,否则文件的位置将有问题。相对路径与Packages混为一谈。 即

  • 问题内容: 我正在尝试从文件中加载图像而不使用。这些文件夹是: 我想将图像加载到资产文件夹中。我试过了: 我已经尝试过使用字符串路径“ /TestProject/assets/drawIcon.png”,但是什么也没有。我不知道如何加载该图片! 问题答案: 将目录设置为资源目录,然后从“ /drawIcon.png”位置将图像加载为资源: 如果要创建javafx图像: 在这种情况下,也请将该文件夹