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 静态变量注入配置文件的更多相关文章

  1. 参考 - spring boot 静态变量注入值

    参考http://blog.csdn.net/zhayuyao/article/details/78553417 @Component public class A { private static ...

  2. spring 给静态变量注入值

    一般在spring中,给static变量加上@Autowired注解的时候会报空指针异常错误. 解决: 1.通过xml配置文件配置 这个就不多说了. 2.通过注解 @Component public ...

  3. spring注解不支持静态变量注入

    spring注解不支持静态变量注入:今天敲代码  自动配置 配置: Animal.java package study01_autoconfig.beanConfig; import org.spri ...

  4. Spring如何给静态变量注入值

    Common.java是一个工具类. Spring无法直接给静态变量注入值,因为静态变量不属于对象,只属于类,也就是说在类被加载字节码的时候变量已经初始化了,也就是给该变量分配内存了,导致spring ...

  5. Spring不能直接@autowired注入Static变量/ 关于SpringBoot的@Autowired 静态变量注入

    昨天在编写JavaMail工具类的时候,静态方法调用静态变量,这是很正常的操作,当时也没多想,直接静态注入. @Component public class JavaMailUtil { @Autow ...

  6. Spring Boot配置,读取配置文件

    Spring Boot配置,读取配置文件 一.配置Spring Boot 1.1 服务器配置 1.2 使用其他Web服务器 1.3 配置启动信息 1.4 配置浏览器显示ico 1.5 Yaml语法 1 ...

  7. Spring Boot 静态资源处理

    spring Boot 默认的处理方式就已经足够了,默认情况下Spring Boot 使用WebMvcAutoConfiguration中配置的各种属性. 建议使用Spring Boot 默认处理方式 ...

  8. Spring Boot 环境变量读取 和 属性对象的绑定

    网上看到的一些方法,结合我看到的 和我们现在使用的.整理成此文: 第一种方法 参见catoop的博客之 Spring Boot 环境变量读取 和 属性对象的绑定(尊重原创) 第二种方法 class不用 ...

  9. Spring boot 自动配置自定义配置文件

    示例如下: 1.   新建 Maven 项目 properties 2.   pom.xml <project xmlns="http://maven.apache.org/POM/4 ...

随机推荐

  1. Vue CLI 3 配置兼容IE10

    最近做了一个基于Vue的项目,需要兼容IE浏览器,目前实现了打包后可以在IE10以上运行,但是还不支持在运行时兼容IE10及以上. 安装依赖 yarn add --dev @babel/polyfil ...

  2. Excel 设置下拉列表

    1. 把列表的候选值写到一块区域, 可以说同Sheet也可以是另一个Sheet中. 2. 选中要设置的列, 选择 Data > Data Validation 3. 在Data Validati ...

  3. SMON进程、PMON进程、LGWR/ARCH

    SMON 进程:system monitor instance monitor 系统监控.实例监控进程 说明及作用:在实例关闭时,会清理临时段,整理空闲空间free space; 实例非正常关闭后,启 ...

  4. liunx服务程序的安装及配置

    1.系统运行级别:

  5. GPIO口的输入输出模式

    1.浮空输入  GPIO_Mode_IN_FLOATING       2.带上拉输入  GPIO_Mode_IPU       3.带下拉输入  GPIO_Mode_IPD       4.模拟输入 ...

  6. [LeetCode&Python] Problem 590. N-ary Tree Postorder Traversal

    Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...

  7. bootstrap中如何控制input的宽度

    ☆1☆ bootstrap中如何控制input的宽度: v2版本:定义了很多class,可用在input. "input-block-level"."input-mini ...

  8. 【mysql】修改数据时候,抛出safe mode相关错误,处理方法

    在mysql5中,可以设置safe mode,比如在一个更新语句中 UPDATE table_name SET bDeleted=0; 执行时会错误,报: You are using safe upd ...

  9. Dataframe 新增一列, apply 通用方法

    df['c'] = df.apply(lambda row: 1 if row['a'] < 0 and row['b'] > 0 else 0, axis=1) apply 是一个好方法 ...

  10. Java Mail多人群发与多附件发送

        近期公司的项目用到了Java Mail来发送注冊邮件,只是.开发的时候都是使用封装好的JAR,曾经也不是非常了解Java Mail的使用原理. 网上非常多代码都是仅仅有一部分,看一看也跑不起来 ...