spring boot 静态变量注入配置文件
spring 静态变量注入
spring 中不支持直接进行静态变量值的注入,我们看一下代码:
@Component(value = "KafkaConfig")
@ConfigurationProperties(prefix = "baseConfig")
public class KafkaConfig {
private static String logBrokerList;
public static String getLogBrokerList() {
return logBrokerList;
}
public static void setLogBrokerList(String logBrokerList) {
KafkaConfig.logBrokerList = logBrokerList;
}
}
配置文件如下:
baseConfig:
logBrokerList: 10.10.2.154:9092
logTopic: test
monitorTopic: monitor
项目启动时使用 logBrokerList变量
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
System.out.println("config static test :" + KafkaConfig.getLogBrokerList());
}
}
执行结果:config static test :null
解决办法
利用spring的set注入方法,通过非静态的setter方法注入静态变量
,我们可以改成这样就静态变量可以获取到你配置的信息了:
@Component(value = "KafkaConfig")
@ConfigurationProperties(prefix = "baseConfig")
public class KafkaConfig {
private static String logBrokerList;
public static String getLogBrokerList() {
return logBrokerList;
}
@Value("${baseConfig.logBrokerList}")
public void setLogBrokerList(String logBrokerList) {
KafkaConfig.logBrokerList = logBrokerList;
}
}
执行结果:config static test :10.10.2.154:9092
spring boot 静态变量注入配置文件的更多相关文章
- 参考 - spring boot 静态变量注入值
参考http://blog.csdn.net/zhayuyao/article/details/78553417 @Component public class A { private static ...
- spring 给静态变量注入值
一般在spring中,给static变量加上@Autowired注解的时候会报空指针异常错误. 解决: 1.通过xml配置文件配置 这个就不多说了. 2.通过注解 @Component public ...
- spring注解不支持静态变量注入
spring注解不支持静态变量注入:今天敲代码 自动配置 配置: Animal.java package study01_autoconfig.beanConfig; import org.spri ...
- Spring如何给静态变量注入值
Common.java是一个工具类. Spring无法直接给静态变量注入值,因为静态变量不属于对象,只属于类,也就是说在类被加载字节码的时候变量已经初始化了,也就是给该变量分配内存了,导致spring ...
- Spring不能直接@autowired注入Static变量/ 关于SpringBoot的@Autowired 静态变量注入
昨天在编写JavaMail工具类的时候,静态方法调用静态变量,这是很正常的操作,当时也没多想,直接静态注入. @Component public class JavaMailUtil { @Autow ...
- Spring Boot配置,读取配置文件
Spring Boot配置,读取配置文件 一.配置Spring Boot 1.1 服务器配置 1.2 使用其他Web服务器 1.3 配置启动信息 1.4 配置浏览器显示ico 1.5 Yaml语法 1 ...
- Spring Boot 静态资源处理
spring Boot 默认的处理方式就已经足够了,默认情况下Spring Boot 使用WebMvcAutoConfiguration中配置的各种属性. 建议使用Spring Boot 默认处理方式 ...
- Spring Boot 环境变量读取 和 属性对象的绑定
网上看到的一些方法,结合我看到的 和我们现在使用的.整理成此文: 第一种方法 参见catoop的博客之 Spring Boot 环境变量读取 和 属性对象的绑定(尊重原创) 第二种方法 class不用 ...
- Spring boot 自动配置自定义配置文件
示例如下: 1. 新建 Maven 项目 properties 2. pom.xml <project xmlns="http://maven.apache.org/POM/4 ...
随机推荐
- linux 读取物理寄存器
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/mman.h ...
- ubunttu-sh: 1: pause: not found
old code: //in ubuntu OS system("pause"); error discription: : pause: not found right code ...
- linux平台模拟生成CAN设备
前言 使用socketCan的过程中有时候没有can接口设备,但是需要测试一下can接口程序是否有问题, 此时需要系统模拟生成can设备,本文介绍linux平台模拟生成CAN设备的方法. 实现步骤 1 ...
- make: *** No rule to make target `/thread_native.h', needed by `ossl.o'. Stop
修改 Makefile 增加 top_srcdir = ../.. 即可 该文件大多存于ruby源文件下 PS:有时也可能是makefile文件多了空格所致
- 初识Tarjan算法
#include<bits/stdc++.h> using namespace std; ; ;//强连通分量的个数 int stk[maxn];//暂时存放遍历过的点,在遇到low[x] ...
- 最小生成树--prim+优先队列优化模板
prim+优先队列模板: #include<stdio.h> //大概要这些头文件 #include<string.h> #include<queue> #incl ...
- Struts2重学习之作用域的获取
第一种:获取requestMap,sessionMap,applicationMap, HttpServletRequest,HttpServletResponse对象的获取,在Struts2中 pu ...
- Hadoop之HDFS
摘要:HDFS是Hadoop的核心模块之一,围绕HDFS是什么.HDFS的设计思想和HDFS的体系结构三方面来介绍. 关键词:Hadoop HDFS 分布式存储系统 HDFS是Hadoop的核心 ...
- stenciljs 学习三 组件生命周期
stenciljs 组件包含好多生命周期方法, will did load update unload 实现生命周期的方法比价简单类似 componentWillLoad ....,使用typescr ...
- dgraph cluster docker-compose 安装
dgraph 是一款基于golang 的图数据库,使用了graphql+ 的查询方式 集群的安装官方也提供了对应的模版,比较简单 docker-compose 文件 我做了一些简单修改(数据存储的问题 ...