Spring 加载Properties文件

  • PropertySource  或者 PropertySources
@Component
@PropertySources(
        {@PropertySource(value={"classpath:Pro.properties"},encoding="utf-8"),
        @PropertySource(value={"classpath:Pro1.properties"},encoding="utf-8")}
        )
public class Dog {

    @Value("${dog.name}")
    private String name;

    @Value("${dog.age}")
    private int age;

    @Override
    public String toString() {
        return "name=="+this.name+" age=="+this.age;

    }
}


public class Test {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext("com.edu.property");
        Dog dog = annotationConfigApplicationContext.getBean(Dog.class);
        System.out.println(dog);
    }
}

 

posted @ 2019-07-17 11:16  兵哥无敌  阅读(224)  评论(0编辑  收藏  举报