Spring中利用配置文件和@value注入属性值

来源:互联网 发布:好看的软妹淘宝店铺 编辑:程序博客网 时间:2024/06/07 20:42

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);      }  }  

转:http://blog.csdn.net/woshixuye/article/details/54999993