spring boot--@Value注解失效
接手一个任务开发预警邮件需求,计划将邮件信息(hostName,用户名,密码,发送方,接受方等)设置为可配置变量,配置在配置中心,使用@Value注解获取配置,如下:
@Value("${warning.email.hostName}")
private String hostName;
@Value("#{'${warning.email.receivers}'.split(',')}")
private List<String> receivers;
@Value("${warning.email.userName}")
private String userName;
@Value("${warning.email.passWord}")
private String passWord;
@Value("${warning.email.sendFrom}")
private String sendFrom;
以上为正确配置方式,发生错误的情况是将这些变量全部声明为了静态变量,导致取值全部为空:
@Value("${warning.email.hostName}")
private static String hostName;
上网查询了下使用@Value的注意事项:
1.不能作用于静态变量(static);
2.不能作用于常量(final);
3.不能在非注册的类中使用(类需要被注册在spring上下文中,如用@Service,@RestController,@Component等);
4.使用这个类时,只能通过依赖注入的方式,用new的方式是不会自动注入这些配置的。
spring boot--@Value注解失效的更多相关文章
- Spring Boot常用注解总结
Spring Boot常用注解总结 @RestController和@RequestMapping注解 @RestController注解,它继承自@Controller注解.4.0之前的版本,Spr ...
- Spring Boot 常用注解汇总
一.启动注解 @SpringBootApplication @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documen ...
- 3个Spring Boot核心注解,你知道几个?
Spring Boot 核心注解讲解 Spring Boot 最大的特点是无需 XML 配置文件,能自动扫描包路径装载并注入对象,并能做到根据 classpath 下的 jar 包自动配置. 所以 S ...
- Spring Boot@Component注解下的类无法@Autowired的问题
title: Spring Boot@Component注解下的类无法@Autowired的问题 date: 2019-06-26 08:30:03 categories: Spring Boot t ...
- Spring boot 基于注解方式配置datasource
Spring boot 基于注解方式配置datasource 编辑 Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...
- 【SpringBoot】15. Spring Boot核心注解
Spring Boot核心注解 1 @SpringBootApplication 代表是Spring Boot启动的类 2 @SpringBootConfiguration 通过bean对象来获取配置 ...
- spring boot纯注解开发模板
简介 spring boot纯注解开发模板 创建项目 pom.xml导入所需依赖 点击查看源码 <dependencies> <dependency> <groupId& ...
- Spring Boot中注解@ConfigurationProperties
在Spring Boot中注解@ConfigurationProperties有三种使用场景,而通常情况下我们使用的最多的只是其中的一种场景.本篇文章带大家了解一下三种场景的使用情况. 场景一 使用@ ...
- 解放生产力:Spring Boot的注解校验
关于对象入参的校验,我们可能第一个想到的就是在Controller层或者Service层增加很多if else的判断,如: if (user.getPassword() == "" ...
- Spring Boot MyBatis注解:@MapperScan和@Mapper
最近参与公司的新项目架构搭建,在使用mybatis的注解时,和同时有了不同意见,同事认为使用@Mapper注解简单明了,而我建议使用@MapperScan,直接将mapper所在的目录扫描进去就行,而 ...
随机推荐
- 结构体中vector的初始化报错:expected identifier before numeric constant
结构体中vector的初始化报错:expected identifier before numeric constant 结构体中vector初始化 起因: 我想在结构体中初始化含有3个元素的vect ...
- SMU Summer 2023 Contest Round 4
SMU Summer 2023 Contest Round 4 A. Telephone Number 满足第一个8后面存在10个字符即可 #include <bits/stdc++.h> ...
- 2023 ECF 游记
录播 闭幕式 回顾 1.11 本来计划下午 vp 去年的 ecf,结果 zjj 忘记买票来投奔我了,所以鸽了.分块和莫队也没看 晚上收拾东西,完全装不下,应该早点寄走一些东西的 1.12 昨晚还想着别 ...
- 从0实现基于Linux socket聊天室-多线程服务器一个很隐晦的错误-2
根据 <0 基于socket和pthread实现多线程服务器模型>所述,server创建子线程的时候用的是以下代码: pconnsocke = (int *) malloc(sizeof( ...
- OpenPCDet为KITTI数据集生成数据信息出现错误TypeError: load() missing 1 required positional argument: ‘Loader‘的解决方案
OpenPCDet为KITTI数据集生成数据信息出现错误 TypeError: load() missing 1 required positional argument: 'Loader' 通过查阅 ...
- React 18 自定义 Hook 获取 useState 最新值
原理:通过同步更新 useRef 来获取最新值 // util.ts export const useRefState = (init: any = null) => { const [sta ...
- 编译 Qt 项目
参考:Qt 编程指南 一个最小化工作示例:qt-minimal | GitHub 源文件 main.cpp #include <QApplication> #include <QLa ...
- 报错解决:partially initialized module 'charset_normalizer' has no attribute 'md__mypyc' (most likely due to a circular import)
在运行jupyter 时候报错'partially initialized module 'charset_normalizer' has no attribute 'md__mypyc' (most ...
- 解决使用filter: blur时图片四周泛白的问题
发现问题 在使用filter: blur(15px)模糊背景图时,发现图片周围会泛白. 解决问题 查了好多办法: 1.使用StackBlur.js处理图片模糊. 2.改变background-size ...
- 【YashanDB知识库】如何远程连接、使用YashanDB?
问题现象 在各个项目实施中,我们经常遇到客户.开发人员需要连接和使用YashanDB但不知如何操作的问题,本文旨在介绍远程连接.使用YashanDB的几种方式. 问题的风险及影响 无风险 问题影响的版 ...