首先新建你的方法类: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. cocos2d-js 热更新具体解释(一)

    本文将会具体解说cocos2d-js下的热更新机制.这篇内容先给大家介绍一下两个manifest文件就当热身了. 首先介绍project.manifest:  举个样例 { "package ...

  2. ZooKeeper伪集群环境搭建

    1.从官网下载程序包. 2.解压. [dev@localhost software]$ tar xzvf zookeeper-3.4.6.tar.gz 3.进入zookeeper文件夹后创建data文 ...

  3. node08---Express框架

    一.Express框架 Express框架是后台的Node框架,所以和jQuery.zepto.yui(雅虎的).bootstrap都不一个东西. Express在后台的受欢迎的程度,和jQuery一 ...

  4. android drawable资源调用使用心得

    1. 调用顺序 android 调用应用图片资源时,会优先选择当前手机屏幕dpi对应的的文件夹(如drawable-ldpi, drawable-mdpi, drawable-hdpi, drawab ...

  5. 采集电脑摄像头和mic,rtp端口推送音视频工具

    介绍:这个是我在做一个rtmp播放的项目中自己写的rtp推送的工具,可选择摄像头,可选择推送rtp的端口和ip 下载地址: github:https://github.com/alexhegang/s ...

  6. Codeforces 701E Connecting Universities 贪心

    链接 Codeforces 701E Connecting Universities 题意 n个点的树,给你2*K个点,分成K对,使得两两之间的距离和最大 思路 贪心,思路挺巧妙的.首先dfs一遍记录 ...

  7. JavaScript学习——JS对象和全局函数

    1. Array对象 数组的特点:长度可变!数组的长度=最大角标+1 2.Boolean对象 如果value 不写,那么默认创建的结果为false 3.Date对象 getTime()返回1970年1 ...

  8. 【原创】VMWare克隆或复制Linux虚拟机后无法上网的解决

    如果选择桥接,需要设置网卡通过哪个物理网卡桥接,桥接代表当前虚拟机通过本机的网卡直接连到网络中,本机网卡作为一个交换机直连.因此需要确定使用哪个网卡桥接,一般在单网卡的时候选择自动即可,多网卡时需要指 ...

  9. jquery定时器

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  10. pthread_cleanup_push

    #define pthread_cleanup_push(func, val) \ { \ struct __darwin_pthread_handler_rec __handler; \ pthre ...