SpringBoot:SpringBoot中@Value注入失败
1. 第一步检测语法是否正确
@Value("${test}")
private String test;
2.第二步检测配置文件中是否有进行配置
url=test
username=username
password=password
3.第三步检测是否增加了@Component注解
在spring中,使用了spring的注解,那么就需要使用spring来进行管理对象,而不能自己进行new,否则就会导致失败。
@Component //将类交给spring进行对象管理
public class DBUtils { @Value("${url}")
private String url;
@Value("${username}")
private String username;
@Value("${password}")
private String password; }
4.第四步检测代码中的书写方式
不要再无参构造器中,进行new对象的操作。否则就会造成@Value注解失败。(本人就是踩到这步坑)。
初始化上下文可以使用@PostConstruct注解来进行初始化,他会在spring加载完信息后,进行调用并且只调用一次。
5.@Value无法注入static属性
使用@Value直接放在static的属性上是无法注入内容的!!!此方式会一直是null.
原因
发现@value不能直接注入值给静态属性,spring 不允许/不支持把值注入到静态变量中;spring支持set方法注入,我们可以利用非静态setter 方法注入静态变量,并且使用@Value的类必须交个spring进行管理.就如同@Autowired有时无法注入一样.
详细说明:https://blog.csdn.net/sqlgao22/article/details/100100314
改进
使用setter方法进行属性的赋值,并且setter方法不能有static
idea自动生成的方法会有static,需要手动删除.
@Component //将类交给spring进行对象管理
public class DBUtils { private static String url;
private static String username;
private static String password; @Value("${url}") //删除掉static
public void setUrl(String url) {
DBUtils.url = url;
}
@Value("${username}")
public void setUsername(String username) {
DBUtils.username = username;
}
@Value("${password}")
public void setPassword(String password) {
DBUtils.password = password;
}
//查看是否注入
public static void get() {
System.out.println("=====url====="+url);
System.out.println("=====username====="+username);
System.out.println("=====password====="+password);
}
}
测试
@RequestMapping("/get")
@ResponseBody
public String get() {
DBUtils.get();
return "get";
}
测试后输出:
成功注入属性.
文章转载至:https://blog.csdn.net/sqlgao22/article/details/100096348
SpringBoot:SpringBoot中@Value注入失败的更多相关文章
- SpringBoot JPA 中无法注入 JpaRepository 接口的问题及解决方案
错误: 在Springboot 框架中使用JPA的过程中,怎么来实现数据库操作底层的交互呢?Spring JPA其实已经提供了一套很全面的解决方案,实现对数据库的增.删.查.改只需要继承JPA实现类 ...
- ssm中mapper注入失败的传奇经历
最近因为要测试一个功能,需要用最短的时间来启动服务,开启测试程序,但平常所用的框架中已经集成了各种三方的东西,想着那就再重新搭建一个最简单的ssm框架吧. 搭建可参考:简单ssm最新搭建 搭建过程并不 ...
- 【Intellij idea】spring中@Autowired注入失败
@Autowired注入失败失败的解决办法? 现有的解决的方案是: 打开file-settings或者ctrl+alt+s -> Editor 然后在Inspections 点击搜索栏 输入Sp ...
- 解决 Springboot中Interceptor拦截器中依赖注入失败
问题: 在Springboot拦截器Interceptor中使用@Resource依赖注入时,发现运行的时候被注解的对象居然是null,没被注入进去 原配置为: @Configurationpubli ...
- 记一个SpringBoot中属性注入失败的问题Consider defining a bean of type ''' in your configuration
今天遇到的一个问题: 代码检查了好几次,都没有错误,但是启动时就会报错Consider defining a bean of type ''' in your configuration. 启动类在c ...
- SpringBoot中service注入失败(A component required a bean of type 'XXService' that could not found)
先写了JUnit,发现启动不了,注释掉有问题的service也不可以.可能是因为spring开始时会加载所有service吧. 按照网友们的说法,一般需要检查: 1.入口类有没有写MapperScan ...
- 关于springboot项目中自动注入,但是用的时候值为空的BUG
最近想做一些web项目来填充下业余时间,首先想到了使用springboot框架,毕竟方便 快捷 首先:去这里 http://start.spring.io/ 直接构建了一个springboot初始化的 ...
- springboot拦截中自动注入的组件为null问题解决方法
一.写SpringUtil类来获取Springh管理的类实例,判断是否注入成功,如果没有注入成功重新获取注入 package com.util; import org.springframework. ...
- Springboot 拦截器 依赖注入失败
解决方案2种. ====1 https://blog.csdn.net/shunhua19881987/article/details/78084679 ====2 https://www.cnblo ...
随机推荐
- Linux进阶之Git分布式版本控制系统篇
一.Git介绍 Git(读音为/gɪt/.)是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理. Git 是 Linus Torvalds 为了帮助管理 Linux 内核 ...
- @JSONField与@DateTimeFormat 注解(Day_21)
@JSONField的常用参数说明 @JSONField(ordinal = 1) //指定json序列化的顺序 @JSONField(serialize = false) //json序列 ...
- fragment textWatcher的设置位置
override fun onStart() { super.onStart() Log.d("------------", "1") val titleWat ...
- Lua学习高级篇
Lua学习高级篇 之前已经说了很多,我目前的观点还是那样,在嵌入式脚本中,Lua是最优秀.最高效的,如果您有不同的观点,欢迎指正并讨论,切勿吐槽.这个系列完全来自于<Programming in ...
- Nginx 配置实例-配置高可用
Nginx 配置实例-配置高可用 1. 实现效果 2. 两台机器 nginx 的安装 2.1 192.168.25.120 中 nginx 的安装 2.1.1 安装 pcre 依赖 2.1.2 安装其 ...
- TensorFlow神经网络集成方案
TensorFlow神经网络集成方案 创造张力流create_tensorflow_neuropod 将TensorFlow模型打包为neuropod包. create_tensorflow_neur ...
- 基于TensorRT的BERT实时自然语言理解(下)
基于TensorRT的BERT实时自然语言理解(下) BERT Inference with TensorRT 请参阅Python脚本bert_inference.py还有详细的Jupyter not ...
- 无网络的win10电脑之间实现相互共享文档
产生需求的原因: 之前做过在有网的情况下,两台win10的电脑怎么在不使用任何第三方软件的情况下实现两者之间的文件共享,但是在完成之后,我猛然间想到一种情况,那就是如果两台wiin10的电脑如果没有网 ...
- 【逆向&渗透实战】Dump内存中的Dex_我是如何脱壳某公司加固过的Apk并利用其API渗透对方数据库
/作者:Kali_MG1937 QQ:3496925334 CNBLOG博客号:ALDYS4/ 某天午睡,朦朦胧胧梦到给学校提供建站模板的公司有个注射点 梦醒后,我凭借着零散的记忆日进了对面的数据库, ...
- 【NX二次开发】多种变换
变换的种类: uf5942 矩阵乘积变换 uf5943 平移变换 uf5944 缩放变换 uf5945 旋转变换 uf5946 镜像变换 最后使用 uf5947 实现uf5942-uf5946的变换. ...