SpringBoot의 PropertySource 메모
3115 단어 초벌 스프링
1. PropertySource 메모에 지정된 속성 파일 로드
Spring 프레임워크는 지정된 속성 파일을 로드하기 위해 PropertySource 메모를 제공합니다.
@Component
@PropertySource(value= {"classpath:config/jdbc-bainuo-dev.properties"},ignoreResourceNotFound=false,encoding="UTF-8",name="jdbc-bainuo-dev.properties",)
public class CustomerDataSourceConfig1 {
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String toString() {
return "CustomerDataSourceConfig{" +
"url='" + url + '\'' +
'}';
}
}
classpath config jdbc-bainuo-dev.properties。 encoding ,ignoreResourceNotFound , jdbc-bainuo-dev.properties。 , ignoreResourceNotFound true , , false.
value , ,name jdbc-bainuo-dev.properties。 springboot , , :“class path resource [config/jdbc-bainuo-dev.properteis]”. classpath:config/jdbc-bainuo-dev.properties, Spring classpath , Resource ClassPathResource。
@ProPertySource name , name : value Resource , Resource getDescription , name 。
jdbc-bainuo-dev.properties :
spring.datasource.shareniu.url=shareniu
application.properties :
spring.profiles.active=dev
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(DemoApplication.class);
ConfigurableApplicationContext configurableApplicationContext = springApplication.run(args);
CustomerDataSourceConfig1 customerDataSourceConfig = configurableApplicationContext
.getBean(CustomerDataSourceConfig1.class);
System.out.print(customerDataSourceConfig);
}
}
, :
CustomerDataSourceConfig{url='null'}
url null, spring.datasource.shareniu.url CustomerDataSourceConfig1 url 。
@Component
@PropertySource( name="jdbc-bainuo-dev.properties",value= {"classpath:config/jdbc-bainuo-dev.properties"},ignoreResourceNotFound=false,encoding="UTF-8")
public class CustomerDataSourceConfig1 {
@Value("${spring.datasource.shareniu.url}")
private String url;
}
ConfigurationProperties
@Component
@ConfigurationProperties(prefix = "spring.datasource.shareniu")
@PropertySource( name="jdbc-bainuo-dev.properties",value= {"classpath:config/jdbc-bainuo-dev.properties"},ignoreResourceNotFound=false,encoding="UTF-8")
public class CustomerDataSourceConfig1 {
private String url;
}
ignoreUnkownFields: 。
ignoreInvalidFields: 。 ? , int , , , true, false。
:1024s