@Value(“${}“) 采坑记录

SpringBoot踩坑记录:玩转@Value注解-自定义PropertySourcesPlaceHolderConfigurer 背景 开发环境:SpringBoot + IDEA 因特殊情况需要,有部分配置项不能直接陪在配置文件properties中,需要从枚举中获取。 这部分配置项以前是配在properties文件中的,现在需要改写到枚举中。 因此service中无法用@Value注解自动注入这些值,需要用新的方式实现注入,且尽量减少代码改动量。 解决方法 ... 阅读详情

背景

今天排查一个老代码bug,最终定位到是配置文件使用@Value注解获取,实际使用时值为null。对@value进行一下用法总结

问题详细说明

我们是一个加密盐存放在application.yml中,工具类中使用如下注解获取,结果为null,大家能看出问题吗?

	@Value("${md5_salt}")
    private static String md5Salt;

问题就在static上,使用static修饰,会在spring初始化之前完成初始,无法注入。

错误用法总结

  1. 使用static或final修饰了tagValue
  private static String tagValue;  //错误
  private final String tagValue;   //错误
  1. 类没有加上@Component(或者@service等)
@Component   //遗漏
class TestValue{
    @Value("${tag}")
    private String tagValue;
}
  1. 类被new新建了实例,而没有使用@Autowired
@Component   
class TestValue{
   @Value("${tag}")
   private String tagValue;
}

class Test{
   TestValue testValue = new TestValue()
}
【SpringBoot】90、SpringBoot中@Value(“${...}“)的使用细节 @Value 注解可以用来将外部的值动态注入到 Bean 中,注入配置文件中的配置参数、注入文件资源、注入URL资源等,我们还可以使用自定义注解实现统一注入管理 阅读详情

相关推荐

@Value(“${fileStorage}“) 不生效排除记录

@Value("${fileStorage}") 不生效排除记录

rqj的专栏 568

@Value("#{}")与@Value("${}")的区别

1 @Value("#{}")   SpEL表达式 @Value("#{}") 表示SpEl表达式通常用来获取bean的属性,或者调用bean的某个方法。当然还有可以表示常量 @RestController @RequestMapping("/login") @Component public class LoginController { @Value("#{1}") private

E_K_in的博客 6万+

RK3588工业开发板实战:如何用IgH EtherCAT主站精准控制伺服电机(附完整代码)

本文详细介绍了如何利用RK3588工业开发板和IgH EtherCAT主站实现伺服电机的精准控制。从硬件选型、实时内核配置到EtherCAT主站安装和核心代码实现,提供了一套完整的工业自动化解决方案,特别适合需要高精度运动控制的场景。

pca5navigator的博客 972

Spring @Value(“#{}“)和@Value(“${}“)

1前言 以前对spring中@Value没有太注意,看到同事把这个注解用的花里胡哨,我就感觉铁子牛逼666呀,迭代版本结束了,我就对这个注解研究研究,争取也牛逼666。 2 #号和$符的不同用法 2.1 $符的用法 总的来说就是:用于获取配置文件中的属性值; 2.1.1简单用法: 通常用于获取写在application.properties中的内容,例如在配置文件中: jdbc.driverClass=com.mysql jdbc.url=3306@local jdbc...

jothan的博客 2429

LabVIEW状态机实战:用STM32+DHT11搭建温湿度监测系统(附完整VI源码)

本文详细介绍了如何利用LabVIEW状态机模式与STM32微控制器、DHT11温湿度传感器构建温湿度监测系统。通过硬件选型、状态机设计、LabVIEW程序实现及系统优化等环节,提供了一套完整的解决方案,并附有VI源码,助力开发者快速实现嵌入式温湿度监测应用。

weixin_42549723的博客 519

玉米群体遗传分析实战:如何用IQtree和VCF文件快速构建系统发育树(附避坑指南)

本文详细介绍了如何利用IQtree和VCF文件进行玉米群体遗传分析,并快速构建可靠的系统发育树。文章重点解析了从VCF格式转换到PHYLIP格式的实用技巧,强调了使用ASC模型校正SNP数据偏差的重要性,并提供了完整的参数设置、常见报错解决方案及结果评估指南,帮助研究者高效完成分析并规避常见陷阱。

leaf8的博客 607

Springboot中@Value的使用详解

Springboot中@Value的使用详解

leveretz的博客 4696

adb 通信协议分析以及实现 (三) (Adb 网络通信部分解析)

这篇文章说下adb 和adb 服务进程之间通信的socket 协议。  adb 服务进程启动之后,会监视本地的5037 端口,使用的是TCP 协议,所以adb 发送的命令其实就是往本地的5037 端口发送数据,做过网络编程的都知道,本机可以用127.0.0.1 ip地址。 有了ip 地址和端口号socket 通信就没问题 1)  命令发送方式      

inforecovvery的博客 2870

SpringBoot @Value注解

直接附在属性名上,在Bean初始化时,会赋初始值。注解将配置文件中的属性注入到容器内组件中(可用在。空时,此时name属性的值为。等Spring托管的类中)没有对应值是,启动报错。

Davis 2471

【3】 灰度变换(1)

第三章(1)【总三】

课题分离 659

AttributeError: module ‘lib‘ has no attribute ‘X509_V_FLAG_CB_ISSUER_CHECK‘解决方案

本文主要介绍了AttributeError: module ‘lib’ has no attribute 'X509_V_FLAG_CB_ISSUER_CHECK’解决方案,希望能对使用python的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 5万+

关于springboot项目配置中@Value的用法

1、常规用法以及不存在取默认值的用法 @Component public class ConfigProperties { @Value("${auth.jwt.token}") private String token; @Value("${auth.jwt.expire}") private Long expire; @Value("${auth.jwt.no:10") private String no; } 2、通过@Value获取list

下半夜的风 2259

《YOLOv8:从入门到实战》专栏介绍 & 专栏目录

本专栏涵盖了丰富的YOLOv8算法从入门到实战系列教程,专为学习YOLOv8的同学而设计,堪称全网最详细的教程!🌈🌈🌈

小哥谈 6489

SpringBoot获取自身tomcat端口号的坑 @Value("${server.port}")

今天在获取springboot自带端口时遇到一个坑,找了好久,故此记录一下。 首先 @Value("${server.port}") 获取端口号没有任何问题,那么问题出在那呢,出在单元测试中!!! 你在单元测试中,调用的类或者方法这个地方获取到的端口号就会变成-1。 解决办法:在后面加上下属性 @SpringBootTest(webEnvironment = SpringBootTest....

weixin_38342534的博客 9201

OpenClaw 2026 路线图详解:3 大协作能力升级与社区贡献增长 62% 的实证分析

介绍 OpenClaw 2026 如何通过上下文锚定(Context Anchoring)、协作质量定义和责任边界机制,将 AI 编程工具从单点补全升级为嵌入代码生命周期的协作对齐系统,并详解 `.openclaw/config.yaml` 中 `context_anchor` 配置的实操方法与效果

weixin_42545292的博客 383

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

@Value和@PropertySource读配置文件采坑留念

一、 网上查到的方式: 直接 @PropertySource加载文件@Value读取属性, Environment.getProperty()获取属性。 结果发现@Value只能拿到"${ips}",获取不到配置文件里的属性。 @Controller @PropertySource("classpath:queryScoreIPList.properties") public class User...

ninefff的博客 2254

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

使用@Value("${xxxx}")注解从配置文件读取值

使用@Value("${xxxx}")注解从配置文件读取值 记录一下自己学习配置文件读取的方法。 假设配置文件为:config.properties 1、从配置文件中读取值的用法: @Value("${user.username}") private String usernsame; 2、在spring的配置文件中进行配置: 方法一(可以在中添加value节点,进行多个配置文件的加载配置): ...

weixin_43714266的博客 4732

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

使用@Value("${}")注解获取配置文件

记录一下使用@Value("${jdbc.driverClass}") 拿配置文件内容踩得坑 1.使用@Value("${}")时接收的类型不能是static修饰的 否则会出现空指针。 解决方法可以写一个set方法: public static String driverClass; @Value("${jdbc.driverClass}") public voi...

qq_41808386的博客 613

SSM框架@Value(“${key}“)不能获取属性,原样输出${key}的分析

SSM框架@Value("${key}")不能获取属性,原样输出${key}的分析

qq_44597856的博客 282

SSM框架@Value("${key}")不能获取属性,原样输出${key}的分析

@Value(&amp;quot;${xxxx}&amp;quot;) 不能获取注入属性

码农飞哥 4183

@Value的诸多用法

搬运地址:@Value用法 留存做学习用 再附上自己学习使用过程中的记录记录 相互印证,发现当初自己只是用到了功能中很小的一部分。。。 对于从事java开发工作的小伙伴来说,spring框架肯定再熟悉不过了。spring给开发者提供了非常丰富的api,满足我们日常的工作需求。 如果想要创建bean实例,可以使用@Controller、@Service、@Repository、@Component等注解。 如果想要依赖注入某个对象,可以使用@Autowired和@Resource注解。 如果想要开启事务,可以

weixin_45614626的博客 3万+

记录一次在使用@Value遇到的坑

首先对类中的静态变量赋值 1、要用到set方法。在方法上使用@Value 2、类上面要用到@Component @Configuration public class SFTPUtil { private transient Logger log = LoggerFactory.getLogger(this.getClass()); /** * 文件目录 */ public static String SIGN; @Value("${confi

qq_30475355的博客 868

2020-07-14 记录@Configuration @Value(${}) 获取配置为null

2020-07-14 记录@Configuration @Value(${}) 获取配置为null (1)遇到问题: 最开始配置 @Configuration @RefreshScope public class ConfigUtils { /** * @Description: url */ @Value("url.crm7002")publicStringcrmUrl7002;@Value("{url.crm7002}") public String crmUrl7002; @Valu

weixin_45608477的博客 3352

spring的@Value注解使用

昨天看到了springMVC的controller中的属性使用了@value注解,并且本人之前用的${}形式,看到别人用的#{}形式,特此记录下。 1.@Value注解作用 该注解的作用是将我们配置文件的属性读出来,有@Value(“${}”)和@Value(“#{}”)两种方式,区别之后介绍,先不多说上图感受。 2.@Value注解作用的两种方式 第一种方式@Value(...

woheniccc的博客 33万+

记录一次SpringBoot中@Value注入失败

1.第一步检测语法是否正确 @Value("${hdfs.name}")private String hdfs; 2.第二步检测配置文件中是否有进行配置(application.properties) hdfs.name=jilin 3.第三步检测是否增加了@Component注解 注意:在spring中,使用了spring的注解,那么就需要使用spring来进行管理对象,而不...

Mrerlou的博客 1372

Spring—@Value在static中引用

@Value在静态代码块,静态方法中引用。 @Value注入原理。 Spring主动加载yml配置

花无名 1万+
上一篇: java正则表达式类用法
下一篇: 多线程并发和并行的区别
鲫鱼哥
博客等级 码龄10年 18粉丝 36原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值