首先新建你的方法类: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. 上机题目(0基础)-计算两个正整数的最大公约数和最小公倍数(Java)

    题目例如以下:

  2. Android.mk添加本地程序和库的经常使用模版

    Android.mk添加本地程序和库的经常使用模版 Android中添加本地程序或者库.这些程序和库与其所在路径没有关系.仅仅与它们的配置文件Android.mk有关.Android.mk文件里可以主 ...

  3. Javaee 应用分层架构

    应用分层的优点:修改方便,仅修改有问题的那层以及其相邻几层即可,层数越多,其相应的资源分配也会更加平均 缺点:耗费时间,速度慢,调用占用大量堆栈. JAVAEE的分层: 4层分法:1.客户层:运行在客 ...

  4. hdu 4882 ZCC Loves Codefires(贪心)

    # include<stdio.h> # include <algorithm> # include <string.h> using namespace std; ...

  5. node14---分层结构数据库操作

    /**回调函数(函数作为参数): 0. 外层函数调用的地方,一定是外层函数体先执行,回调函数和普通函数地址一样,然后看函数体规定回调函数怎么执行. 1. 异步时候使用回调函数, 无论是否异步,回调函数 ...

  6. python解压,压缩,以及存数据库的相关操作

    zipfile实现压缩整个目录和子目录 import os,shutil,zipfile,glob def dfs_get_zip_file(input_path,result): # files = ...

  7. bzoj3295: [Cqoi2011]动态逆序对(cdq分治+树状数组)

    3295: [Cqoi2011]动态逆序对 题目:传送门 题解: 刚学完cdq分治,想起来之前有一道是树套树的题目可以用cdq分治来做...尝试一波 还是太弱了...想到了要做两次cdq...然后伏地 ...

  8. SAS拆分数据集

    2012年8月8日 主要是根据选取条件来进行拆分 1.根据行数来选: data test; set oldset; if _n_=10 then output; if id="001&quo ...

  9. quartz.net定时任务框架详解

    C#做定时任务:一是Timer:而是Quartz.net:本文就学习一下Quartz.net框架 Quartz.net非常的灵活,开发人员能用很少的代码就能完成“每隔一小时执行”.“每天2点执行”.“ ...

  10. latex简历遇到的问题

    博一时候简历就没弄出来,现在又要用了,于是找出当初的模板.发现问题在于编码. \XeTeXinputencoding "GBK" \XeTeXdefaultencoding &qu ...