How to setting PropertySourcesPlaceholderConfigurer Auto SetLocations for External Configuration in Java Spring Boot

huangapple go评论104阅读模式
英文:

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")
    };

huangapple
  • 本文由 发表于 2020年5月5日 16:57:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/61609398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定