@Value注解必须要在spring的bean中才能使用,不能自己new一个对象调用

产生原因:

  • 在SpringBoot中使用@Value只能给普通变量赋值,不能给静态变量赋值

解决方法:

  • 给静态变量增加一个非静态的set方法,注意需要把@Value注解写到对应的set方法上,而不是定义的静态变量上,如下所示:
@Value("${authox.sql.url}")
public void setUrl(String url) {JdbcUtils.url = url;
}@Value("${authox.sql.username}")
public void setUser(String user) {JdbcUtils.user = user;
}@Value("${authox.sql.password}")
public void setPassword(String password) {JdbcUtils.password = password;
}@Value("${authox.sql.driver-class-name}")
public void setDriver(String driver) {JdbcUtils.driver = driver;
}

一、    碰到过三种情况导致@Value获取不到配置值

  • 变量被关键字static修饰
  • 类没有使用@Component及其衍生标签修饰
  • 在Bean初始化时构造方法中引用被@Value修饰的变量

需要获取的配置如下

kafka:bootstrap:servers: 192.168.202.131:9092servers:first:topic: "first_topic"group: "first_group"

测试项目是springboot的,如果是普通spring项目的application.properties文件也是类似

2. 变量被关键字static修饰

package com.coline.codeskills.kafka;import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;/*** @author: Coline* @ClassName: TestValue* @Date: 2020/3/1 23:58* @Description: test @Value Tag*/
@Component
public class TestValue implements InitializingBean {@Value("${kafka.bootstrap.servers}")private String kafkaServers;@Value("${kafka.servers.first.topic}")private static String topic;@Overridepublic void afterPropertiesSet() throws Exception {System.out.println(kafkaServers);System.out.println(topic);}
}

可以看到被static修饰的参数使用@Value无法获取到配置值

3. 类没有使用@Component及其衍生标签修饰

因为@Value是在AbstractAutowireCapableBeanFactory类的doCreateBean方法中进行初始化,即交由Spring容器进行处理,如果没有@Component及其衍生注解注释Spring无法识别,导致无法获取到配置值。

4. 在Bean初始化时构造方法中引用被@Value修饰的变量

package com.coline.codeskills.kafka;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;/*** @author: Coline* @ClassName: TestValue* @Date: 2020/3/1 23:58* @Description: test @Value Tag*/
@Component
public class TestValue {@Value("${kafka.bootstrap.servers}")private String kafkaServers;@Value("${kafka.servers.first.topic}")private static String topic;public TestValue(){System.out.println(kafkaServers);}}

如图,bean初始化时在构造方法中无法获取到@Value修饰的参数值

解决方法:

如果需要在bean初始化时获取参数值,那么我们可以使用@Config注解在bean初始化时获取到,代码如下:

package com.coline.codeskills.common.config;import com.coline.codeskills.kafka.TestValue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;/*** @author: Coline* @ClassName: TestValueConfig* @Date: 2020/3/2 0:44* @Description: TODO*/
@Configuration
public class TestValueConfig {@Value("${kafka.bootstrap.servers}")private String kafkaServers;@Primary@Beanpublic TestValue testValue(){TestValue testValue = new TestValue();return testValue;}}

Spring中@Value注解获取不到配置值相关推荐

  1. @Value注解获取不到配置值

    1. 写在前面 碰到过三种情况导致@Value获取不到配置值 变量被关键字static修饰 类没有使用@Component及其衍生标签修饰 在Bean初始化时构造方法中引用被@Value修饰的变量 如 ...

  2. spring中自定义注解(annotation)与AOP中获取注解___使用aspectj的@Around注解实现用户操作和操作结果日志

    spring中自定义注解(annotation)与AOP中获取注解 一.自定义注解(annotation) 自定义注解的作用:在反射中获取注解,以取得注解修饰的类.方法或属性的相关解释. packag ...

  3. spring中的注解和xml配置文件中配置对应总结

    spring中的注解和xml配置文件中配置对应 需要导入的jar spring-context spring-context-support spring-test commons-logging b ...

  4. Spring中异步注解@Async的使用、原理及使用时可能导致的问题

    前言 最近,很多同学碰到了下面这个问题,添加了Spring提供的一个异步注解@Async循环依赖无法被解决了,下面是一些读者的留言跟群里同学碰到的问题: 本着讲一个知识点就要讲明白.讲透彻的原则,我决 ...

  5. Spring中常用注解的介绍

    spring中使用注解时配置文件的写法: <?xml version="1.0" encoding="UTF-8"?> <span style ...

  6. 声明式事务、Spring 中常用注解、Ajax

    五. 声明式事务 编程式事务: 1.1 由程序员编程事务控制代码. 1.2 OpenSessionInView 编程式事务 声明式事务: 先引入依赖 <dependency><gro ...

  7. Spring中@Value注解详解

    在spring项目中必不可少的就是读取配置文件,那么读取配置文件就有两种方式.一种就是使用Spring中@Value注解,还有一种是使用SpringBoot中的@ConfigurationProper ...

  8. Spring中@AliasFor注解的作用及原理

    本文基于Springboot 2.1.6.RELEASE 版本分析. 关于@AliasFor注解,曾提过的一个issue Explicit attribute overrides configured ...

  9. Spring 中所有注解

    Spring中的注解主要分为两类: 类级别的注解: 如@Component.@Repository.@Controller.@Service以及JavaEE6的@ManagedBean和@Named注 ...

最新文章

  1. 新一届最强预训练模型上榜,出于BERT而胜于BERT
  2. python3调用腾讯AI开放平台
  3. JS进阶篇--JS数组reduce()方法详解及高级技巧
  4. php api 无符号整数基数为16的整数参数的字符串表示形式,[1.12]-参数规则:接口参数规则配置 | PhalApi(π框架) - PHP轻量级开源接口框架 - 接口,从简单开始!...
  5. Android Studio出现UnsupportedClassVersionError Unsupported major.minor version 52.0
  6. VirtualBox中出现UUID have already exists 解决方法
  7. linux下redis的安装和配置
  8. 推荐算法概述(01)
  9. bzoj 3513: [MUTC2013]idiots【生成函数+FFT】
  10. 25 款软件上榜,2020“最佳开源奖” 出炉!
  11. Elasticsearch 并发修改乐观锁
  12. HDU2076 夹角有多大【水题】
  13. Django-(分页器,中间介)
  14. 传统的BIOS启动过程与UEFI启动过程
  15. Windows权限维持方法
  16. 阿里云产品推荐——云数据库RDS MySQL 版
  17. vscode常用插件 - Path Autocomplete
  18. 终于有人把 p 值讲明白了!
  19. 【中国人口金字塔2019,python,pandas,matplotlib,numpy 】
  20. 购买PG霜,请认准官方网站

热门文章

  1. 小A最多新会认识多少人
  2. mysql登录/退出命令
  3. 如何有效掌握标签打造优质单品,如何掌握人群标签的秘诀
  4. MATLAB课程设计音频信号处理,MATLAB音频信号处理实验
  5. 哲学家就餐问题C语言实现(涉及多线程、信号量等)
  6. 关于新浪微博里面长图加载功能的一点窥探
  7. MySQL视图/存储器/触发器
  8. C语言数组初始化及malloc初始化
  9. c语言将数组初始化为1_c语言之数组初始化
  10. 退休当月要干到月底吗_法定退休年龄到了当月还要交社保吗