1. 程式人生 > >spring boot could not resolve placeholder in string value 問題解決方法

spring boot could not resolve placeholder in string value 問題解決方法

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'hosts' in string value "${db.hosts}"

問題的產生是由於有多個properties檔案造成的,如果再第一個properties檔案中沒有找,就不認為沒有了,不繼續找下一個properties檔案

解決辦法如下:

方法 一、

在Application中加入一個靜態方法

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;


@Configuration
@SpringBootApplication
@ComponentScan
public class TestApplication {


public final static void main(String[] args) {
SpringApplication.run(VfcadaptorApplication.class, args);
}

@Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer();
        c.setIgnoreUnresolvablePlaceholders(true);
        return c;
    }
}

方法二、 統一為屬性name加字首

<span lang="EN-US" font-size:11.5pt;font-family:consolas;"="">app.datasource.foo.type=daffaDataSource
app.datasource.foo.status =30
那就需要在類檔案上加註解 

@ConfigurationProperties("app.datasource.foo")

@ConfigurationProperties("app.datasource.foo")

Publicclass AA{

PrivateString type;

PrivateString

status;

...

}