英文:
How to setting PropertySourcesPlaceholderConfigurer Auto SetLocations for External Configuration in Java Spring Boot
问题
我对如何设置外部配置的位置感到困惑,根据 https://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/html/boot-features-external-config.html 进行了一些外部配置,但是失败了。以下是我的代码:
package com.org.tre.myth.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
@Configuration
public class ExternalPropertyConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
final PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
Resource[] resources = new Resource[] {
new ClassPathResource("/myth/app/data/weblogic_configuration/config/conf.properties"), // 我希望在开发部署时使用此配置
new FileSystemResource("src/main/resources/conf.properties") // 我希望在本地测试时使用此配置
};
properties.setLocations(resources);
properties.setIgnoreResourceNotFound(true);
properties.setIgnoreUnresolvablePlaceholders(false);
return properties;
}
}
它总是在 FileSystemResource 上运行,当我部署时,它不会自动从 ClassPathResource 中读取配置,而在开发环境中希望这样做。我的配置实际上具有相同的内容(dev 和 test),不同之处只在于目录,因为我的开发环境进行了一些分层,我希望配置能够自动运行,而无需为 Spring 创建配置文件。
抱歉,我更新了代码如下:
Resource[] resources = new Resource[] {
new FileSystemResource("/myth/app/data/weblogic_configuration/config/conf.properties"),
new FileSystemResource("src/main/resources/conf.properties")
};
英文:
I'am confused how to set location for External Configuration,
based on https://docs.spring.io/spring-boot/docs/1.2.2.RELEASE/reference/html/boot-features-external-config.html i do some external configuration but fail, Here is my code
package com.org.tre.myth.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
@Configuration
public class ExternalPropertyConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
final PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
Resource[] resources = new Resource[ ] {
new ClassPathResource("/myth/app/data/weblogic_configuration/config/conf.properties"), // i want this config used when i deploy it in dev
new FileSystemResource("src/main/resources/conf.properties") // i want this config used when in local test
};
properties.setLocations( resources );
properties.setIgnoreResourceNotFound(true);
properties.setIgnoreUnresolvablePlaceholders(false);
return properties;
}
}
it's always running on FileSystemResource, when i deploy, it doesn't wanna to read automatically from ClassPathResource when on dev.
My config actually have same content (dev&test), the different is just at the directory because my dev do some layering and i want the configuration automatically running without even made a profile for spring.
I'am sorry
UPDATE
Im using this
Resource[] resources = new Resource[ ] {
new FileSystemResource("/myth/app/data/weblogic_configuration/config/conf.properties"),
new FileSystemResource("src/main/resources/conf.properties")
};
答案1
得分: 0
使用这个
Resource[] resources = new Resource[] {
new FileSystemResource("/myth/app/data/weblogic_configuration/config/conf.properties"),
new FileSystemResource("src/main/resources/conf.properties")
};
英文:
Using this
Resource[] resources = new Resource[ ] {
new FileSystemResource("/myth/app/data/weblogic_configuration/config/conf.properties"),
new FileSystemResource("src/main/resources/conf.properties")
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论