spring boot could not resolve placeholder in string value 问题解决方法

来源:互联网 发布:linux安装tgz文件 编辑:程序博客网 时间:2024/05/21 05:17

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;

...

}


阅读全文
0 0
原创粉丝点击