springboot学习(3)-配置文件的导入:PropertySource、ImportResource、Configuration

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}
发布了40 篇原创文章 · 获赞 1 · 访问量 4479

猜你喜欢

转载自blog.csdn.net/hyt182380/article/details/103810851