Java 外部化Spring引导资源的问题
我试图将一些财产和资源外部化;具体来说,是一个属性文件和一个json文件 我有一个配置类:Java 外部化Spring引导资源的问题,java,spring,spring-boot,Java,Spring,Spring Boot,我试图将一些财产和资源外部化;具体来说,是一个属性文件和一个json文件 我有一个配置类: @Configuration @PropertySource("sheets.properties") public class SheetsConfiguration { @Bean(name="sheets.secrets") public GoogleClientSecrets clientSecrets(GoogleClientSecret
@Configuration
@PropertySource("sheets.properties")
public class SheetsConfiguration {
@Bean(name="sheets.secrets")
public GoogleClientSecrets clientSecrets(GoogleClientSecretsFactory clientSecretsFactory,
@Value("${sheets.credentials.file}") String credentialFileName) {
return clientSecretsFactory.buildWithResourceCredentials(credentialFileName);
// which calls GoogleClientSecretsFactory.class.getResourceAsStream(credentialsFilePath);
// ${sheets.credentials.file} should resolve to "/sheets.json"
}
//...
}
我的启动目录如下所示:
root@foobar:~/# ls ~ -al
total 34292
drwxr-xr-x 4 root root 4096 Mar 8 19:23 .
drwxr-xr-x 3 root root 4096 Mar 7 19:39 ..
-rw-r--r-- 1 root root 35086447 Mar 8 20:50 foo.jar
drwxr-xr-x 2 root root 4096 Mar 8 19:23 logs
-rw-r--r-- 1 root root 436 Mar 8 20:51 sheets.json
-rw-r--r-- 1 root root 91 Mar 8 20:51 sheets.properties
@PropertySource("file:${FOO_PATH}sheets.properties")
根据关于外部化配置的文档,我认为应该是这样的
root@foobar:~/#java-jar~/foo.jar--spring.config.location=file:~/sheets.properties
或
root@foobar:~/#java-jar~/foo.jar--spring.config.additional location=文件:~/sheets.properties
或
root@foobar:~/#java-jar~/foo.jar--spring config name=application,sheets--spring.config.location=file:~/
但每次:
2021-03-08 20:51:13,598 ERROR [org.springframework.boot.SpringApplication] Application run failed org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [foo.Main]; nested exception is java.io.FileNotFoundException: class path resource [sheets.properties] cannot be opened because it does not exist
...
Caused by: java.io.FileNotFoundException: class path resource [sheets.properties] cannot be opened because it does not exist
如果资源与jar一起打包,那么一切都可以正常工作,但是凭证信息显然应该外部化 在包含spring boot的外部属性和资源时,我忽略了什么
root@foobar:~/#
java-jar~/foo.jar--spring.config.name=sheets.properties
默认情况下,Spring Boot将根据配置文件的放置位置覆盖配置文件。但是,默认情况下,它会尝试将名为application.properties
或application.yml
的文件作为属性文件加载。因为您有一个自定义属性文件,所以必须声明它
至于文件位置。外部配置文件覆盖内部文件。因此,如果您在jar中的资源中有一个sheets.properties
,那么当前目录中的外部文件将覆盖它。你不需要任何特殊的财产
因此,如果您使用上述命令执行Jar,您当前目录中的属性文件将被加载
检查以下链接以了解如何应用外部化配置,以及spring boot在默认情况下尝试从哪些目录加载属性
在类中使用属性文件的绝对路径,如下所示:
root@foobar:~/# ls ~ -al
total 34292
drwxr-xr-x 4 root root 4096 Mar 8 19:23 .
drwxr-xr-x 3 root root 4096 Mar 7 19:39 ..
-rw-r--r-- 1 root root 35086447 Mar 8 20:50 foo.jar
drwxr-xr-x 2 root root 4096 Mar 8 19:23 logs
-rw-r--r-- 1 root root 436 Mar 8 20:51 sheets.json
-rw-r--r-- 1 root root 91 Mar 8 20:51 sheets.properties
@PropertySource("file:${FOO_PATH}sheets.properties")
然后在启动应用程序时设置其值:
java-jar-DFOO\u PATH=/your/project/PATH/~/foo.jar
在windows上使用
\\
作为路径分隔符。刚刚尝试过。同样的结果。此外,您链接到的文档是针对Spring Boot 1.0.1的;我在问题中链接的2.1.9文档中没有说明这种行为,我是否应该看一些特别的东西来解释这种行为?您能否尝试在您的项目资源中放置一个空的sheets.properties
?并检查是否也加载了当前目录中被重写的一个。也许它只是在类路径中需要一个,然后当发现它是基本的,并且被其他更高层次的人覆盖时,这几乎正是我所需要的!我需要@PropertySource(“file:sheets.properties”)
,这对添加工作表很好。在没有指定“file”的情况下,Spring默认只查看类路径。