gpt4 book ai didi

spring-boot - tomcat 中的 Spring boot WAR - 外部化 application.properties

转载 作者:行者123 更新时间:2023-11-28 21:57:35 33 4
gpt4 key购买 nike

我有一个 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 extends 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

最佳答案

documentation有几个重要的注意事项:

spring.config.name and spring.config.location are used very early to determine which files have to be loaded, so they must be defined as an environment property (typically an OS environment variable, a system property, or a command-line argument).

If spring.config.location contains directories (as opposed to files), they should end in / (and, at runtime, be appended with the names generated from spring.config.name before being loaded, including profile-specific file names). Files specified in spring.config.location are used as-is, with no support for profile-specific variants, and are overridden by any profile-specific properties.

When custom config locations are configured by using spring.config.location, they replace the default locations.

最后

If you have specified any files in spring.config.location, profile-specific variants of those files are not considered. Use directories in spring.config.location if you want to also use profile-specific properties.

所以

  • spring.config.location 中使用目录而不是文件可能更好
  • 将您的目录添加到默认位置,除非您不需要它们。
  • 确保目录以斜杠结尾。
  • 您还需要使用协议(protocol) file: 来引用类路径之外的文件。

总而言之,这应该可行:在 Tomcat 的 setenv.sh 脚本中导出一个环境变量

export SPRING_CONFIG_LOCATION=classpath:/,classpath:/config/,file:./,file:./config/,file:/app/shared/conf/

或使用系统属性(-Dspring.config.location)或命令行标志(--spring.config.location)代替。

关于spring-boot - tomcat 中的 Spring boot WAR - 外部化 application.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56535997/

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