@Value注解位于spring-beans中,以下是@Value注解的源码:
package org.springframework.beans.factory.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Value {
    String value();
}
 
由上可以看出:
@Value可以获取配置文件中的值,设置给属性,也可以引用Bean的属性值。下面通过SpringBoot项目讲解@Value的用法。
使用@Value引用配置文件中的属性值的方式为
@Value(“${属性名}”)
application.yml文件的配置如下:
ymlname: only-yml student: name: yml里的name age: 20 tel : 666
application.properties文件的配置如下:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestValueController {
    // 只在application.yml中配置
    @Value("${ymlname}")
    private String ymlname;
    // 只在application.properties中配置
    @Value("${propname}")
    private String propname;
    // application.yml和application.properties均有该配置
    @Value("${student.name}")
    private String name;
    // 配置文件中的字段名和属性名不一致
    @Value("${student.age}")
    private int nianling;
    // application.yml和application.properties均没有该配置,设置默认值
    @Value("${student.score:100}")
    private int score;
    // application.yml有该配置,同时设置默认值
    @Value("${student.tel:888}")
    public int tel;
    @ResponseBody
    @RequestMapping(value = "/test")
    public String testValue() {
        return "ymlname —— " + ymlname + "
" +
                "propname —— " + propname + "
" +
                "name —— " + name + "
" +
                "nianling —— " + nianling + "
" +
                "score —— " + score + "
" +
                "tel —— " + tel;
    }
}
 
启动SpringBoot项目,浏览器输入localhost:8080/test,界面显示如下。

正常情况下 @Value不可作用于静态属性。如下例。
启动类上做如下修改:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootTestApplication {
    @Value("${student.name}")
    public static String name;
    public static void main(String[] args) {
        SpringApplication.run(SpringbootTestApplication.class, args);
        System.out.println("name: " + name);
    }
}
 
打印结果如下:

通过上例可以看出,使用@Value注解修饰静态属性,启动项目时不会报错,但是也不会给该静态属性设置值。
可以通过set方法给静态属性设置配置文件中的属性值。
    public static String name;
    
    @Value("${student.name}")
    public void setName(String param) {
    	name = param;
    }
 
使用@Value引用Bean的属性值的方式和引用配置文件中的属性值方式类似。使用方式为
@Value(“#{bean的名字.属性值}”)
以通过@Value注解引用User实例的name属性值为例;
User类:
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class User {
    private String name;
    private String password;
}
 
配置一个TestConfig类,用于产生一个name为zhangsan,password为66666的名为user的bean实例交由spring容器管理。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfig {
    @Bean(name = "user")
    public User getUser() {
        return new User("zhangsan","66666");
    }
}
 
TestBeanPro 类用于测试,其有一个userName属性,通过@Value注解将容器中名为user的bean的name属性注入给userName。@PostConstruct注解的方法于该类的构造方法执行完成后执行。在本例中,该初始化方法用于打印user的name属性是否引用成功。
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Slf4j
@Component
public class TestBeanPro {
    @Value("#{user.name}")
    private String userName;
    @PostConstruct
    public void init() {
        log.info("***************************** userName:{}.", userName);
    }
}
 
启动项目后通过控制台日志可以看到,userName的值为zhangsan。控制台日志如下

从以上测试结果可以看出:
通过对@Value的以上分析,我们还不难看出,SpringBoot加载配置文件的顺序为.yml > .properties。即yml类型的优先级高于properties类型的配置文件。
最近做项目的时候,给static变量赋值, 使用 @value注解 ,结果 获取一直为null ,
1、spring不允许/不支持把值注入到静态变量中 2、Spring的@Value依赖注入是依赖set方法 3、set方法是普通的对象方法 4、static变量是类的属性,static没有set方法
例如,application-dev.properties配置文件有如下配置:

给普通变量赋值时,直接在变量声明之上添加@Value()注解即可,如下所示:

当要给静态变量注入值的时候,若是在静态变量声明之上直接添加@Value()注解是无效的,例如:

虽然没有编译和运行上的报错,经调试可知这种注解方式mailUsername、mailPassword、mailHost的值都是null,也就是说直接给静态变量读取配置文件是无效的,如下所示: 
若要给静态变量赋值,可以使用set()方法,其中需要在类上加入@Component注解,方法名(例如setMailUsername)和参数名(例如username)可以任意命名,如下所示:
调试结果如下:

@Component
public class JDConfig {
 
 
    /** 转换系统地址 */
 
    public static String url;
    /** 转换系统应用系统id */
 
    public static String sysId;
    /** 是否开启鉴权 */
 
    public static Boolean isAuth;
    /** 转换系统应用系统秘钥(如开启鉴权需要填写) */
 
    public static String sysKey;
 
    @Autowired(required = false)
    @Value(value="${jd.serverHost:}")
    public void setUrl( String url) {
        JDConfig.url = url;
    }
 
    @Autowired(required = false)
    @Value(value="${contract.jd.appKey:}")
    public void setSysId( String sysId) {
        JDConfig.sysId = sysId;
    }
    @Autowired(required = false)
    @Value(value="${jd.isAuth:true}")
    public void setAuth(Boolean isAuth) {
        JDConfig.isAuth = isAuth;
    }
 
    @Autowired(required = false)
    @Value(value="${contract.jd.appSecurity:}")
    public void setSysKey(String sysKey) {
        JDConfig.sysKey = sysKey;
    }
 
    public  String getUrl() {
        return url;
    }
 
    public  String getSysId() {
        return sysId;
    }
 
    public  Boolean getIsAuth() {
        return isAuth;
    }
 
    public  String getSysKey() {
        return sysKey;
    }
}
 
如果你觉得@value注解麻烦。可以使用@ConfigurationProperties注解代替,这样比较简洁
最近的项目还有有这样一个需求,就是类中有几个静态变量,初始化的时候,他们的值需要读取一个配置文件,获取一个code,然后用这个code拼接而成。 这个code不是静态的变量,怎么实现的呢,代码如下:
    @Value("${projectCode}")
    private String projectCode;
 
    public static String COOPERATIVE_GOV_TEMPLATE_KEY ;
    // 消息短信配置
    public static String DEPOLY_KEY;
    // 消息短信详情配置
    public static String MSG_DEPOLY_KEY;
    // 过滤配置
    public static String MSG_FILTER_KEY;
 
    @PostConstruct
    public void init() {
        COOPERATIVE_GOV_TEMPLATE_KEY = projectCode + ":template";
        DEPOLY_KEY = projectCode + ":depoly";
        MSG_DEPOLY_KEY = projectCode + ":msgDepoly";
        MSG_FILTER_KEY = projectCode + ":msgFilter";
    }
 
这样当项目启动的时候,这几个静态变量就有有值了。 一定要注意这个类要被spring管理,也就是要用@Controller,@Service,@Component等注解注释。

那么问题来啦!我们什么场景下需要把值注入到静态变量?
场景一

场景二
工具类中将值注入静态变量,就可以直接在静态方法之中使用,我本文中遇到的正是这个场景
 @Component
 public class FileComponent {
       public static Boolean enabledInline;
 	  public static String inlineWebUrl;
 	  @Value("${file.upload.enabledInline}")
       public void setEnabledInline(Boolean enabledInline) {
    		this.enabledInline = enabledInline;
       }
 	  @Value("${file.upload.inlineUrl}")
 	  public void setInlineWebUrl(String inlineWebUrl) {
    		this.inlineWebUrl = inlineWebUrl;
 	  }
  }