结论先行
@Value可以给类属性赋值(包括实际值Spel即#{},和${}从配置文件取值)
@PropertyScource加载配置文件
XML配置导入配置文件
<context:property-placeholder location="classpath:person.properties"/>
bean定义
package com.ouyangxizhu.bean;
import org.springframework.beans.factory.annotation.Value;
public class PersonValue {
private String name;
private int age;
private String nickName;
public PersonValue() {
}
public PersonValue(String name, int age, String nickName) {
this.name = name;
this.age = age;
this.nickName = nickName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
@Override
public String toString() {
return "PersonValue{" +
"name='" + name + '\'' +
", age=" + age +
", nickName='" + nickName + '\'' +
'}';
}
}
配置类
package com.ouyangxizhu.config;
import com.ouyangxizhu.bean.PersonValue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MainConfigValuePropertySource {
@Bean
public PersonValue personValue() {
return new<