我有一个Spring Boot应用程序,该应用程序打包为WAR,并部署到了tomcat中。 由于存在一些遗留问题,因此采用了这种方式进行部署。
我在将application.properties外部化时遇到问题
DevOps团队要求文件位于/ app / shared / conf /
下
我发现了有关此主题的许多问题,但似乎没有一个解决方案...
这是我到目前为止尝试过的:
添加到tomcat的Context.xml:
<Parameter name="spring.config.location" value="/app/shared/conf/application.properties" />
添加到startup.sh:
export spring_config_location=/app/shared/conf/
添加到startup.sh:
export SPRING_CONFIG_LOCATION=/app/shared/conf/application.properties
添加到startup.sh中,并在SecurityConfig上进行设置,从而扩展了WebSecurityConfigurerAdapter
CLASSPATH=/app/shared/conf/
@PropertySource("classpath:application.properties")
已添加到应用程序主类:
System.setProperty("spring.config.location", "/app/shared/conf/application.properties");
添加到SecurityConfig扩展了WebSecurityConfigurerAdapter
@PropertySource("file:/app/shared/conf/application.properties")
这是我不断收到的错误:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.0.M1)
11-Jun-2019 02:54:22.034 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/prntPrtl]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:986)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThrexeroxoolExecutor.runWorker(ThrexeroxoolExecutor.java:1142)
at java.util.concurrent.ThrexeroxoolExecutor$Worker.run(ThrexeroxoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.xerox.printHub.app.PrinterPortal]; nested exception is java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:181)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:315)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:705)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:785)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:407)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:322)
at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:157)
at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:137)
at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:91)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:171)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5204)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 10 more
Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:159)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:99)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:73)
at org.springframework.core.io.support.PropertiesLoaderUtils.loxeroxroperties(PropertiesLoaderUtils.java:59)
at org.springframework.core.io.support.ResourcePropertySource.<init>(ResourcePropertySource.java:67)
at org.springframework.core.io.support.DefaultPropertySourceFactory.createPropertySource(DefaultPropertySourceFactory.java:37)
at org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:452)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:271)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:191)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:295)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:242)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:199)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167)
... 26 more
答案 0 :(得分:2)
documentation有几个重要说明:
spring.config.name和spring.config.location很早就用于确定必须加载哪些文件,因此必须将它们定义为环境属性(通常是OS环境变量,系统属性或命令)行参数)。
和
如果spring.config.location包含目录(而不是文件),则它们应以/结尾(并且在运行时,应在加载之前附加从spring.config.name生成的名称,包括特定于配置文件的文件)名称)。在spring.config.location中指定的文件按原样使用,不支持特定于配置文件的变体,并且会被任何特定于配置文件的属性覆盖。
和
使用spring.config.location配置自定义配置位置时,它们将替换默认位置。
最后
如果您在spring.config.location中指定了任何文件,则不考虑这些文件的特定于配置文件的变体。如果您还想使用特定于配置文件的属性,请使用spring.config.location中的目录。
所以
spring.config.location
中的文件file:
来引用类路径之外的文件。总之,这应该可行:在Tomcat的setenv.sh
脚本中导出环境变量
export SPRING_CONFIG_LOCATION=classpath:/,classpath:/config/,file:./,file:./config/,file:/app/shared/conf/
或使用系统属性(-Dspring.config.location
)或命令行标志(--spring.config.location
)。