1. 程式人生 > 其它 >Springboot多屬性檔案配置

Springboot多屬性檔案配置

Springboot 多屬性檔案配置

配置檔案字尾有兩種: .properties.yml

要完成多屬性配置需要自定義PropertySourcesPlaceholderConfigurer 這個Bean

properties配置方法

    /**
     * 這裡必須是static函式
     * 如果不是 application.propertise 將讀取不到
     * application.properties 是預設載入的,這裡配置自己的properties就好
     * @return
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        // properties 載入方式
        Resource[] resources = {
                //new ClassPathResource("application.properties"),
          			//ClassPathResource針對的是resource目錄下的檔案
                new ClassPathResource("test.properties")
        };
        configurer.setLocations(resources);
        return configurer;
    }

注意這個Bean 的函式必須是static的,否則會載入不到application.properties中的內容

這裡不需要將application.properties也加進來,因為application.properties是預設加進來的,這裡只要寫其他的屬性檔案就好了

yml配置方法

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();

        // yml 載入方式
				YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
      	//這裡可以傳入多個自定義屬性檔案
				yaml.setResources(new ClassPathResource("test.yml"));
				configurer.setProperties(yaml.getObject());

        configurer.setLocations(resources);
        return configurer;
    }

yml 和properties的配置的注意點是一樣的,只是需要YamlPropertiesFactoryBean來載入

2. 如果引用到屬性檔案中的值

test.propertise

grady.username=jiang
grady.password=1234567

TestConfig.java

@Configuration
// 這裡不需要了
//@PropertySource("classpath:test.properties")
public class TestConfig {

   @Value("${grady.username}")
   private String username;

   @Value("${grady.password}")
   private String password;

   public String getUsername() {
       return username;
   }

   public String getPassword() {
       return password;
   }
}

這裡用@Configuration或@Component都可以獲得到值,

注意:這裡不需要使用@PropertySource了,直接用就可以了

3. 在Controller中使用(其他地方也可,這裡是舉例)

public class UserController {

   @Autowired
   private TestConfig testConfig;

   @Autowired
   private SystemConfig systemConfig;

   @PostMapping("/hello")
   public String Hello() {
       String datasourcePassword = systemConfig.getDatasourcePassword();
       return "Hello World" + testConfig.getUsername() + "  " + testConfig.getPassword()
               + "  datasourcePassword= " + datasourcePassword;
   }

postMan中的結果

Hello Worldjiang  1234567  datasourcePassword= Root123#

4. 更簡潔的寫法

@Configuration
public class SystemConfig {

    private static List<Resource> resourceList = new ArrayList<>();

    static {
        resourceList.add(new ClassPathResource("test.properties"));
        resourceList.add(new ClassPathResource("jdbc.properties"));
    }

    @Value("${spring.datasource.password}")
    private String datasourcePassword;

    /**
     * 這裡必須是static函式
     * 如果不是 application.propertise 將讀取不到
     * application.properties 是預設載入的,這裡配置自己的properties就好
     * @return
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        // properties 載入方式
        configurer.setLocations(resourceList.stream().toArray(Resource[]::new));
        return configurer;
    }

    public String getDatasourcePassword() {
        return datasourcePassword;
    }
}