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方便使用 但是如果直接注入会发现无法注入而报空指针异常 解决方法: 在注册 ...
随机推荐
- Collection 和 Collections 的差别?
Collection 是 java.util 下的接口,它是各种集合的父接口,继承于它的 接口主要有 Set 和 List:Collections 是个 java.util 下的类.是针对集合的 帮助 ...
- intellij idea 13&14 插件推荐及高速上手建议 (已更新!)
早些年 在外企的时候,公司用的是intellij idea ,当时也是从eclipse.MyEclipse转过去的非常是不习惯. 用了一周明显感觉爱上它了.由于它非常智能,并且能纠正你非常多不好的习惯 ...
- HDU 5353 Average
Problem Description There are n soda sitting around a round table. soda are numbered from 1 to n and ...
- iOS 自己主动登录,登录过程中一直显示载入页
iOS开发中 假设client做的人性化一点肯定会考虑自己主动登录 事实上原理非常easy,就是再首次登录成功之后将username和password存入userdefault 下次登录的时候推断us ...
- ubuntu12.04下NFS链接开发板并测试交叉编译的第一个应用
思路:配置网络->安装NFS->配置NFS->挂载NFS服务->Down文件执行.Okay lets go! 配置网络: 在配置网络之前,首先咱得搞定与开发板的交互工作,那么这 ...
- C语言-实现字符串倒序输出
方法1: Action(){//倒序输出 char *src="abcdefgh123"; char *desc; desc=(char *)malloc(100*sizeof(c ...
- 函数式编程-只用"表达式",不用"语句"()
把函数当作普通的运算符使用. 2. 只用"表达式",不用"语句"() "表达式"(expression)是一个单纯的运算过程,总是有返回值: ...
- SQL Server查询死锁,杀死进程解决死锁
查询死锁进程和表 SELECT request_session_id AS spid , OBJECT_NAME(resource_associated_entity_id) AS 'table' F ...
- 【LNOI2014】【BZOJ3626】NOIp2018模拟(三) LCA
Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1.设$dep[i]$表示点i的深度,$lca(i,j)$表示i与j的最近公共祖 ...
- C语言的常用printf打印占位符%d, %u, %f, %s, %c, %o, %x
占位符含义及用法 代码: #include <stdio.h> int main(int argc, char const *argv[]) { , b = -; // 默认10进制赋值 ...