PropertySource:读取指定的配置文件,位置需要引用配置文件的地方,注意配合ConfigurationProperties进行内容的引用:
@ConfigurationProperties(prefix ="person")
@PropertySource(value = {"classpath:person.properties"})
@Component
public class Person {
private String name;
private int age;
private boolean boss;
private Map<String,Object> maps;
private List<String> list;
private Dog dog;
public Person() {
}
ImportResource导入配置文件到容器中:
//@ImportResource(value = {"classpath:beans.xml"})
@SpringBootApplication
public class YmlconfigApplication {
public static void main(String[] args) {
SpringApplication.run(YmlconfigApplication.class, args);
}
}
Configuration:通过此注解可以将指定的类作为配置文件,配置@Bean注解将bean注入到容器中:
@Configuration
public class ServiceConfig {
//容器中bean的名称就是该方法名helloService
@Bean
public Helloservice helloService(){
return new Helloservice();
}
}
1.配置文件中的参数可以通过${参数名}在配置文件中引用
person.name=hyt
person.dog.name=xiaocat
person.list=[1,,3,s]
person.maps.x1=x1
person.maps.x2=${person.name}