1 简单属性值注入
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service // 需要被注入属性值的类需要被Spring管理
public class PropertiesService1 {
// 利用@Value注解,即使没有该属性或者属性文件也不会报错
// @Value输入属性值name,默认值xydefault
@Value("${name:xydefault}")
private String name;
// @Value输入属性值num,默认值-1
@Value("${num:-1}")
private Integer num;
// @Value输入属性值type,默认值-2
@Value("${type:-2}")
private Integer type;
@Value("#{testPro}")
private Properties pros;
public void getInfo() {
System.out.println("name:" + name + ",num:" + num + ",type:" + type);
}
}