@Value不支持给静态变量或者静态方法赋值,例如下面这两种情况
@Value("${stratedy.enable}")
private static String enable;
=====================================================
@Value("${stratedy.enable}")
public static setEnable( String enable) {
EquipmentStrategy.enable = enable;
}
原因是因为 启动扫描的时候会扫描有没有static修饰符,有的话会输出Autowired annotation is not supported on static fields (静态字段不支持自动连接注释)
AutowiredAnnotationBeanPostProcessor:
// 构建@Autowired注入元数据方法
// 简单的说就是找到该Class类下有哪些是需要做依赖注入的
private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
...
// 循环递归,因为父类的也要管上
do {
// 遍历所有的字段(包括静态字段)
ReflectionUtils.doWithLocalFields(targetClass, field -> {
if (Modifier.isStatic(field.getModifiers())) {
logger.info("Autowired annotation is not supported on static fields: " + field);
}
return;
...
});
// 遍历所有的方法(包括静态方法)
ReflectionUtils.doWithLocalMethods(targetClass, method -> {
if (Modifier.isStatic(method.getModifiers())) {
logger.info("Autowired annotation is not supported on static methods: " + method);
}
return;
...
});
...
targetClass = targetClass.getSuperclass();
} while (targetClass != null && targetClass != Object.class);
...
}
这几句代码道出了Spring为何不给static静态字段/静态方法执行@Autowired注入的最真实原因:扫描Class类需要注入的元数据的时候,直接选择忽略掉了static成员(包括属性和方法)。
在Java中,针对static静态成员,我们有一些最基本的常识:静态变量(成员)它是属于类的,而非属于实例对象的属性;同样的静态方法也是属于类的,普通方法(实例方法)才属于对象。而Spring容器管理的都是实例对象,包括它的@Autowired依赖注入的均是容器内的对象实例,所以对于static成员是不能直接使用@Autowired注入的。
这很容易理解:类成员的初始化较早,并不需要依赖实例的创建,所以这个时候Spring容器可能都还没“出生”,谈何依赖注入呢?
第一种解决办法,将注解打在变量的赋值方法上,前提是这个方法必须是实例方法(非静态的方法)
还有一个前提,这个类必须交给Spring管理,需要类上有@Component注解,
private static String enable;
@Value("${stratedy.enable}")
public void setEnable(String enable) {
EquipmentStrategy.enable = enable;
}
第二种方法@PostConstruct注入 Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法。被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。
private static String enable;
@Value(${stratedy.enable})
private String s;
@PostConstruct
public void init(){
enable = s;
}
第三种方法 实现InitializingBean 覆写aftePropertiesSet方法
//InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法
public class Person implements InitializingBean {
private static String enable;
@Value(${stratedy.enable})
private String s;
@Override
public void afterPropertiesSet() throw Exception {
enable = s;
}
}
@Value这个注解可以修饰变量、方法、方法参数、注解上面!
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Value {
/**
* The actual value expression such as <code>#{systemProperties.myProp}</code>
* or property placeholder such as <code>${my.app.myProp}</code>.
*/
String value();
}
@Component注解,项目启动,spring会创建EquipmentStrategy对象,紧接着就会把@value注解处理了,该类就可以别的地方使用@Autowired注入了
以上三种方式必须使用@Component,还有些情况,比如工具类不想交给spring管理,还有另外的方法,注入Environment env,使用evn.getProperty(“startegy.enable”),普通类无法注入bean,需要使用工具类注入
private static Environment evn = SpringContextHolder.getBean(Environment.class);
private static Environment env = SpringUtil.getBean(Environment.class);
public static void test() {
// 获取yml中的value
System.out.println( env.getProperty("startegy.enable"));
}
工具类 可以直接使用
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
public SpringUtil() {
}
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
if (applicationContext == null) {
applicationContext = arg0;
}
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static void setAppCtx(ApplicationContext webAppCtx) {
if (webAppCtx != null) {
applicationContext = webAppCtx;
}
}
/**
* 拿到ApplicationContext对象实例后就可以手动获取Bean的注入实例对象
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
public static <T> T getBean(String name, Class<T> clazz) throws ClassNotFoundException {
return getApplicationContext().getBean(name, clazz);
}
public static final Object getBean(String beanName) {
return getApplicationContext().getBean(beanName);
}
public static final Object getBean(String beanName, String className) throws ClassNotFoundException {
Class clz = Class.forName(className);
return getApplicationContext().getBean(beanName, clz.getClass());
}
public static boolean containsBean(String name) {
return getApplicationContext().containsBean(name);
}
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return getApplicationContext().isSingleton(name);
}
public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
return getApplicationContext().getType(name);
}
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return getApplicationContext().getAliases(name);
}
public static String getActiveProfile() {
if (applicationContext == null) {
throw new RuntimeException("applicationContext属性为null,请检查是否注入了SpringContextHolder!");
}
return applicationContext.getEnvironment().getActiveProfiles()[0];
}
}
Value(“#{}”) ,$注解从配置文件读取值的用法,也就是从application.yaml文件中获取值,
@Value(“#{}”)是获取bean属性,系统属性,同样也需要Bean交给spring
//@Component("chen") bean的名字 chen
@Component("chen")
@Data
public class Test {
public String shuang(){
return "测试成功";
}
}
=================================================
@RestController
@RequestMapping("/test")
public class TestController<T> {
// 这里的bean的名字只能输入chen,自动转换的,和注入不一样
@Value("#{chen.shuang}")
private String ggg;
@GetMapping
public String get() {
return ggg;
}
}
// 测试成功
还有一种 @Value(“chen”),没有$也没有#,直接给变量赋值
@Value("哈哈")
private String test;
@GetMapping
public String get() {
return test;
}
// 输出“哈哈” ,相当于直接给test变量赋值