spring@value取不到值的几种情况
一,spring组件重写构造方法,在构造方法中引用@value为null
由于spring实例化顺序为先执行构造方法,再注入成员变量,所以序为先执行构造方法,再注入成员变量,所以ing实例化顺取值为null
解决办法为:再写一个常量类,在常量类中引用@value,再在构造方法中引用常量类的变量即可。
二,调用spring组件时使用new对象,而不是@Autowired
三,使用final或static修饰成员变量
四,spring mvc中引用@value为null
spring mvc是spring的子容器,需要在两个配置文件中都导入配置文件
<context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>
http://blog.51cto.com/jtech/2114686
springboot @ConfigurationProperties @EnableConfigurationProperties 自动配置
通过redis自动配置过程看这两个注解的联合使用,达到自动配置
springboot项目,maven
application.properties
配置redis
...
spring.redis.port=7379
...
需要获取配置的类,比如@Configuration注解的配置类,需要获取redis配置的其他类,redisProperties.getHost() 就可以取到配置
import org.springframework.boot.autoconfigure.data.redis.RedisProperties
@EnableConfigurationProperties(RedisProperties.class)
class JedisMy{
@Autowired
private RedisProperties redisProperties
}
获取使用内置的Environment获取配置,需要指定配置的key,上面的自动配置,key就是类的属性,redis的配置都是RedisProperties的属性
import org.springframework.core.env.Environment
class ddd{
@Autowired
private Environment env;
public void dd(){
env.getProperty("spring.redis.port");
}
}
---------------------
作者:qianggetaba
来源:CSDN
原文:https://blog.csdn.net/c5113620/article/details/81383297
版权声明:本文为博主原创文章,转载请附上博文链接!
关与 @EnableConfigurationProperties 注解
@EnableConfigurationProperties
测试发现 @ConfigurationProperties
与 @EnableConfigurationProperties
关系特别大。
@EnableConfigurationProperties
文档中解释:
当@EnableConfigurationProperties
注解应用到你的@Configuration
时, 任何被@ConfigurationProperties
注解的beans将自动被Environment属性配置。 这种风格的配置特别适合与SpringApplication的外部YAML配置进行配合使用。
测试发现:
1.使用 @EnableConfigurationProperties
进行注册
@ConfigurationProperties(prefix = "service.properties")
public class HelloServiceProperties {
private static final String SERVICE_NAME = "test-service";
private String msg = SERVICE_NAME;
set/get
}
@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello", value = "enable", matchIfMissing = true)
public class HelloServiceAutoConfiguration {
}
@RestController
public class ConfigurationPropertiesController {
@Autowired
private HelloServiceProperties helloServiceProperties;
@RequestMapping("/getObjectProperties")
public Object getObjectProperties () {
System.out.println(helloServiceProperties.getMsg());
return myConfigTest.getProperties();
}
}
自动配置设置
service.properties.name=my-test-name
service.properties.ip=192.168.1.1
service.user=kayle
service.port=8080
一切正常,但是 HelloServiceAutoConfiguration 头部不使用 @EnableConfigurationProperties
,测访问报错。
2.不使用 @EnableConfigurationProperties
进行注册,使用 @Component
注册
@ConfigurationProperties(prefix = "service.properties")
@Component
public class HelloServiceProperties {
private static final String SERVICE_NAME = "test-service";
private String msg = SERVICE_NAME;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
Controller 不变,一切正常,如果注释掉 @Component 测启动报错。
由此证明,两种方式都是将被 @ConfigurationProperties 修饰的类,加载到 Spring Env 中。
作者:咪雅先森
链接:https://www.jianshu.com/p/7f54da1cb2eb
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
Spring @Value 设置默认值
1. 概述
在 Spring 组件中使用 @Value 注解的方式,很方便的读取 properties 文件的配置值。
2.使用场景
声明的变量中使用。
public static class FieldValueTestBean { @Value("#{ systemProperties['user.region'] }")
private String defaultLocale; }
setter 方法中
public static class PropertyValueTestBean { private String defaultLocale; @Value("#{ systemProperties['user.region'] }")
public void setDefaultLocale(String defaultLocale) {
this.defaultLocale = defaultLocale;
} }
方法中
public class SimpleMovieLister { private MovieFinder movieFinder;
private String defaultLocale; @Autowired
public void configure(MovieFinder movieFinder,
@Value("#{ systemProperties['user.region'] }") String defaultLocale) {
this.movieFinder = movieFinder;
this.defaultLocale = defaultLocale;
} // ...
}
构造方法
public class MovieRecommender { private String defaultLocale; private CustomerPreferenceDao customerPreferenceDao; @Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao,
@Value("#{systemProperties['user.country']}") String defaultLocale) {
this.customerPreferenceDao = customerPreferenceDao;
this.defaultLocale = defaultLocale;
} // ...
}
3. 字符串
字符串类型的属性设置默认值
@Value("${some.key:my default value}")
private String stringWithDefaultValue;
some.key 没有设置值,stringWithDefaultValue 变量值将会被设置成 my default value 。
如果默认值设为空,也将会被设置成默认值。
@Value("${some.key:}")
private String stringWithBlankDefaultValue;
4. 基本类型
基本类型设置默认值。
@Value("${some.key:true}")
private boolean booleanWithDefaultValue; @Value("${some.key:42}")
private int intWithDefaultValue;
包装类型设置默认值。
@Value("${some.key:true}")
private Boolean booleanWithDefaultValue; @Value("${some.key:42}")
private Integer intWithDefaultValue;
5. 数组
数组的默认值可以使用逗号分割。
@Value("${some.key:one,two,three}")
private String[] stringArrayWithDefaults; @Value("${some.key:1,2,3}")
private int[] intArrayWithDefaults;
6. SpEL
使用 Spring Expression Language (SpEL) 设置默认值。
下面的代码标示在systemProperties属性文件中,如果没有设置 some.key 的值,my default system property value 会被设置成默认值。
@Value("#{systemProperties['some.key'] ?: 'my default system property value'}")
private String spelWithDefaultValue;
7.结语
上面讲解使用 Spring @Value 为属性设置默认值。在项目中,提供合理的默认值,在大多情况下不用任何配置,就能直接使用。达到零配置的效果,降低被人使用的门槛。简化新Spring应用的搭建、开发、部署过程。
8.参考链接
Using Spring @Value with Defaults
Annotation-based configuration
https://blog.csdn.net/vcfriend/article/details/79700048
spring@value取不到值的几种情况的更多相关文章
- Spring中@Value("${}"))取不到值的几种情况
https://blog.csdn.net/dh12313012/article/details/84661169 1. spring组件重写构造方法,在构造方法中引用@Value为null 由于sp ...
- Spring如何使用JdbcTemplate调用存储过程的三种情况
注:原文 <Spring如何使用JdbcTemplate调用存储过程的三种情况 > Spring的SimpleJdbcTemplate将存储过程的调用进行了良好的封装,下面列出使用Jdbc ...
- 自己遇到的ajax调用ashx文件无法获取返回值的一种情况
无法获取返回值的ashx文件大致如下: public void ProcessRequest (HttpContext context) { context.Response.ContentType ...
- 探讨read的返回值的三种情况
http://blog.chinaunix.net/uid-23629988-id-3035613.html 今天探讨一个很看似简单的API “read”的返回值问题.read的返回值有哪几个值?每个 ...
- go语言 函数return值的几种情况
分三种情况 (以下 “指定返回值”这句话, 仅指return后面直接跟着的返回值) 退出执行,不指定返回值 (1) 函数没有返回值 package main import ( "fmt&qu ...
- [jQ/PHP]使用JS数组储值的两种情况(提交PHP处理)
---------------------------------------------------------------------------------------------------- ...
- @Value取不到值引出的spring的2种配置文件applicationContext.xml和xxx-servlet.xml
项目中经常会用到配置文件,定义成properties的形式比较常见,为了方便使用一般在spring配置文件中做如下配置: <context:property-placeholder ignore ...
- spring注解@Value取不到值【转】
spring注解@Value取不到值 今天在一个项目中发现一个情况,在Service中取不到name值,直接输出了{name}字符串,找了好久,最后在一篇文章中找到解决方案. 解决这个问题的一篇文章( ...
- @Value取不到值的原因(引用application.properties中自定义的值)
在spring mvc架构中,如果希望在程序中直接使用properties中定义的配置值,通常使用一下方式来获取: @Value("${tag}") private String ...
随机推荐
- Javascript 对象创建多种方式 原型链
一.对象创建 1.new Object 方式 直接赋上属性和方法 var obj = new Object(); obj.name = '娃娃'; obj.showName = function(){ ...
- 解决rpm conflicts with file from package的两个方法
1.卸载掉冲突的文件,安装新的文件.如果由于由于依赖关系导致要卸载很多软件,那可以优先考虑下一个方法. 2.安装的时候增加–replacefiles参数,例如 rpm -ivh xxx.rpm –re ...
- 1.Django入门
MVC 大部分开发语言中都有MVC框架 MVC框架的核心思想是:解耦 降低各功能模块之间的耦合性,方便变更,更容易重构代码,最大程度上实现代码的重用 m表示model,主要用于对数据库层的封装 v表示 ...
- [控件]unigui移动端下Unidatepicker时间显示解决方案
[控件]unigui移动端下Unidatepicker时间显示解决方案 http://tz10000.com/kong-jian-unigui-yi-dong-duan-xia-unidatepick ...
- 1*1卷积核在GoogleLeNet中的作用
1. 实现跨通道的交互和信息整合 1×1的卷积层(可能)引起人们的重视是在NIN的结构中,论文中林敏师兄的想法是利用MLP代替传统的线性卷积核,从而提高网络的表达能力.文中同时利用了跨通道poolin ...
- 简单理解 OAuth 2.0 及资料收集,IdentityServer4 部分源码解析
简单理解 OAuth 2.0 及资料收集,IdentityServer4 部分源码解析 虽然经常用 OAuth 2.0,但是原理却不曾了解,印象里觉得很简单,请求跳来跳去,今天看完相关介绍,就来捋一捋 ...
- MVVM Light 新手入门(1):准备阶段
1.新建WPF空白项目. 2.NuGet 程序包中安装 3.根据MVVM分层结构,建立包含Model.View.ViewModel三层文件夹 如图: 1.View负责前端展示,与ViewModel进行 ...
- 为什么要使用Entity Framework
本文介绍从DDD(Domain-Driven Design[领域驱动设计])的角度来说说为什么要使用Entity Framework(以下都会简称为EF),同时也看出类似Drapper之类的简陋ORM ...
- 【BZOJ4755】 [Jsoi2016]扭动的回文串
BZOJ4755 [Jsoi2016]扭动的回文串 Solution 考虑对于他给出的 A中的一个回文串: B中的一个回文串: 或者某一个回文的扭动字符串S(i,j,k) 这样子几个限制,我们1,2就 ...
- 自定义SpringBoot控制台输出的图案
pringboot启动的时候,控制台输出的图案叫banner banner?啥玩意儿?相信有些人,一定是一脸懵逼... ——这个就不陌生了吧,这个是我们启动springboot的时候,控制台输出的.. ...