首先新建你的方法类: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变量赋值的更多相关文章

  1. springBoot 动态注入bean(bean的注入时机)

    springBoot 动态注入bean(bean的注入时机) 参考博客:https://blog.csdn.net/xcy1193068639/article/details/81517456

  2. SpringBoot 中使用 @Value 为 static 变量赋值

    原文:https://www.jianshu.com/p/ea477fc9abf7 例如: public class Utils { @Value("${test.host}") ...

  3. spring boot 中用@value给static变量赋值

    需求:改写一个JedisUtils,工具类,所以最好用静态方法和变量. @value("${redis.host}") private static String redisHos ...

  4. @Value注解无法为static 变量赋值

    使用@Value给静态变量赋值时,出现空指针异常.经了解Spring 不允许/不支持把值注入到静态变量中.所以需要另一种方式为该变量赋值. 需要注意set方法也不要加static修饰符!

  5. springboot 静态方法注入service

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 16.0px; font: 14.0px Arial; color: #3f3f3f; bac ...

  6. SpringBoot动态注入Bean

    目的: 在程序运行期间,动态添加Bean进入到Spring容器. 目前使用到的场景: 对当当网的ElasticJob进行封装,通过自定义注解@ElasticJob的方式开启分布式定时任务. 当所有的B ...

  7. Springboot依赖注入 Service类中使用静态变量

    @Component public class ServerHandler extends IoHandlerAdapter { @Autowired protected HealthDataServ ...

  8. 静态方法中注入bean

    @Componentpublic class ScriptExecuteContent { @Autowired private static SignRepository signRepositor ...

  9. SpringBoot拦截器中无法注入bean的解决方法

    SpringBoot拦截器中无法注入bean的解决方法 在使用springboot的拦截器时,有时候希望在拦截器中注入bean方便使用 但是如果直接注入会发现无法注入而报空指针异常 解决方法: 在注册 ...

随机推荐

  1. call to OpenGL ES API with no current context 和Fatal signal 11

    近日在用cocos2dx3.4的时候使用了JNI调用,发现一个现象 当不使用jni的时候全然正常.使用了jni后回去的全部文字都变成黑块,而且有概率程序崩溃.附带出了两个log call to Ope ...

  2. 微软柯塔娜(Cortana)的一句名言

    近日.媒体频传Win 10装机量已经超过多少千万台.我操心的问题是,集成在Win 10操作系统中的柯塔娜小姐将怎样面对各色各样的人群.由于,在这个世界上.人是最复杂的生物,什么人都有. 依据国外媒体报 ...

  3. org.hibernate.AssertionFailure: null id in com.you.model.User entry (don't flush the Session after a

    1.错误描写叙述 org.hibernate.AssertionFailure: null id in com.you.model.User entry (don't flush the Sessio ...

  4. 编写shell脚本获取本机的网络地址。  比方:本机的ip地址是:192.168.100.2/255.255.255.0,那么它的网络地址是 192.168.100.1/255.255.255.

    ubuntu@ubuntu:~$ vim getlocalip.sh #!/bin/bash #ifconfig | grep inet | awk '$2' | awk -F : '$2' IP=` ...

  5. hdu_2191多重背包

    用二维数组解的,因为忘了memset害我wa了好几发... #include<iostream> #include<cstdio> #include<cstring> ...

  6. bzoj3173: [Tjoi2013]最长上升子序列(树状数组+二分倒推)

    3173: [Tjoi2013]最长上升子序列 题目:传送门 题解:  好题! 怎么说吧...是应该扇死自己...看错了两次题: 每次加一个数的时候,如果当前位置有数了,是要加到那个数的前面,而不是直 ...

  7. Compiler Warning (level 2) CS0436

    https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0436 // CS0436_a.cs // compile with: /target:l ...

  8. [luogu P3813] [FJOI2017] 矩阵填数 解题报告 (容斥原理)

    题目链接: https://www.luogu.org/problemnew/show/P3813 题目: 给定一个 h*w的矩阵,矩阵的行编号从上到下依次为 1..h,列编号从左到右依次1..w. ...

  9. HTML5学习笔记(一):HTML5基本概念

    1.HTML的发展历程 HTML(1994年,W3C成立) HTML2(1995年) HTML3(1996年) HTML4.0(1997年) HTML4.01(1999年)——HTML5(2008年: ...

  10. VSCode向上的代码提示消除

    VSCode虽然好用, 但是有些用户体验实在非常差, 比如这种往上面弹的类型提示... 在用户设置中增加: "editor.parameterHints": false