springboot 静态方法注入bean、使用@value给static变量赋值
首先新建你的方法类:DemoUtil
头部加注解:@Component
@Component
public class DemoUtil {
}
新增静态变量:
static DemoService demoService;
新增@Autowired的bean对象
@Autowired
DemoService demoServiceMapping;
注意这时候还是不能注入
新增@PostConstruct注解方法
@PostConstruct
public void init() {
demoService = demoServiceMapping;
}
当然还需要注意的就是启动类的扫描范围:
不同包下可在启动类加上@ComponentScan配置扫描范围
properties 属性引入
假若在properties文件中配置如下属性:project.author
之前有写到用@value
那么在静态方法中怎么引入呢,直接@value是不可以的,一下介绍两种方法
第一种:如上文注入bean方式,在@PostConstruct方法中配置使用
第二种:
public static String springProfilesActive; @Value(value = "${spring.profiles.active}")
public void setSpringProfilesActive(String springProfilesActive) {
SystemConfig.springProfilesActive = springProfilesActive;
}
使用set方法赋值,set方法static去掉,这种方法也必须注意启动类的扫描范围。
springboot 静态方法注入bean、使用@value给static变量赋值的更多相关文章
- springBoot 动态注入bean(bean的注入时机)
springBoot 动态注入bean(bean的注入时机) 参考博客:https://blog.csdn.net/xcy1193068639/article/details/81517456
- SpringBoot 中使用 @Value 为 static 变量赋值
原文:https://www.jianshu.com/p/ea477fc9abf7 例如: public class Utils { @Value("${test.host}") ...
- spring boot 中用@value给static变量赋值
需求:改写一个JedisUtils,工具类,所以最好用静态方法和变量. @value("${redis.host}") private static String redisHos ...
- @Value注解无法为static 变量赋值
使用@Value给静态变量赋值时,出现空指针异常.经了解Spring 不允许/不支持把值注入到静态变量中.所以需要另一种方式为该变量赋值. 需要注意set方法也不要加static修饰符!
- springboot 静态方法注入service
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 14.0px Arial; color: #3f3f3f; bac ...
- SpringBoot动态注入Bean
目的: 在程序运行期间,动态添加Bean进入到Spring容器. 目前使用到的场景: 对当当网的ElasticJob进行封装,通过自定义注解@ElasticJob的方式开启分布式定时任务. 当所有的B ...
- Springboot依赖注入 Service类中使用静态变量
@Component public class ServerHandler extends IoHandlerAdapter { @Autowired protected HealthDataServ ...
- 静态方法中注入bean
@Componentpublic class ScriptExecuteContent { @Autowired private static SignRepository signRepositor ...
- SpringBoot拦截器中无法注入bean的解决方法
SpringBoot拦截器中无法注入bean的解决方法 在使用springboot的拦截器时,有时候希望在拦截器中注入bean方便使用 但是如果直接注入会发现无法注入而报空指针异常 解决方法: 在注册 ...
随机推荐
- HDU 4335 Contest 4
利用降幂公式..呃,还是自己去搜题解吧.知道降幂公式后,就不难了. #include <iostream> #include <cstdio> #include <alg ...
- pintos操作系统thread部分的实现
pintos是斯坦福大学自己开发的一个教学用操作系统.里面的代码给我们留了很多坑.我们的目标就是解决这些坑!详细的实现大家能够看看这篇blog,尽管我的代码并非所有跟着他写的,可是这确实是一篇非常好地 ...
- Hive分区表与分桶
分区表 在Hive Select查询中.通常会扫描整个表内容,会消耗非常多时间做不是必需的工作. 分区表指的是在创建表时,指定partition的分区空间. 分区语法 create table tab ...
- Ruby学习笔记(一)
在做实验的时候,由于每次都要手工修改文件夹的名字,实在是给自己添了太多的麻烦,为了摆脱手工修改的困恼,于是产生了一个使用程序批量修改文件夹名字的好主意.为了最终实现这个目标,自然需要选择一种合适的脚本 ...
- Linux开放1521端口允许网络连接Oracle Listener
症状: 1. TCP/IP连接是通的.可以用ping 命令测试. 2. 服务器上Oracle Listener已经启动. lsnrctl status 查看listener状态 lsnrct ...
- BZOJ 1898 构造矩阵+矩阵快速幂
思路: T的最小公倍数是12 那么12以内暴力 整除12 的部分用矩阵快速幂 //By SiriusRen #include <cstdio> #include <cstring&g ...
- Photoshop CC (2015.5) 2016.6 版已发布
Photoshop CC (2015.5) 2016.6 版已发布 adobe-cc-no-more-direct-download-links.html 不再有直接下载的升级包了,不开心 :( 下载 ...
- 读 Real-Time Rendering 收获 - chapter 4. transform
chapter 4. Transform p54 affine transform p57 all rotation matrices have a determinant of one and ar ...
- gcd的queue与group
queue相当于事件处理机制里的事件池:只是任务池: 线程作为事件处理的实施者,由线程池从任务池中获取任务进行调度派发: group相当与工作组,按照任务的相关性对任务进行组织.
- Thread Control Block
Thread Control Block The following is the declaration of the Thread Control Block. struct tcb { u32_ ...