我们使用注解注入数据,比如
@Component
public class Person {
@Value("zhangsan")
public String name;
@Value("20")
public int age;
@Value("2000/1/2")
public Date birthday;
//Getter,Setter,toString略
}
这样子会把数据写死,比较好的方法是使用配置文件,我们在resources文件夹下新建一个person.properties
person.name=zhangsan
person.age=20
person.birthday=2000/1/2
我们只需要加入@PropertySource
注解和EL表达式即可
@PropertySource("classpath:person.properties")
@Component
public class Person {
@Value("${person.name}")
public String name;
@Value("${person.age}")
public int age;
@Value("${person.birthday}")
public Date birthday;
//Getter,Setter,toString略
}