参考:https://blog.csdn.net/qq_35056292/article/details/78430777

问题出现:

在一个非controller/service类中,我需要注入Config类

@Autowired
MyConfig myConfig; public int getUrl(){
String url=myConfig.getUrl;
}

这时候,myConfig是null

解决方法:

@Component  // 关键1,将该工具类注册为组件
public class TokenUtil {
@Autowired
MyConfig myConfig; public static TokenUtil tokenUtil; // 关键2 public TokenUtil() {
} // 关键3
@PostConstruct
public void init() {
tokenUtil = this;
tokenUtil.tokenRepository = this.tokenRepository;
}

使用的时候,改成

public String getUrl(){
MyConfig MyConfig=TokenUtil.tokenUtil.myConfig;
String url= myConfig.getUrl;
}

Spring @Autowired注解在非Controller/Service中注入为null的更多相关文章

  1. Spring @Autowired注解在非Controller中注入为null

    问题描述 今天在写一个工具类,里面用了@Autowired注入了StringRedisTemplate以及RedisTemplate时,在template.opsForValue().set(key, ...

  2. Spring boot @Autowired注解在非Controller中注入为null

    参考链接:https://blog.csdn.net/qq_35056292/article/details/78430777

  3. Spring @Autowired 注解 学习资料

    Spring @Autowired 注解 学习资料 网址 Spring @Autowired 注解 https://wiki.jikexueyuan.com/project/spring/annota ...

  4. JAVA CDI 学习(5) - 如何向RESTFul Service中注入EJB实例

    RESTFul Service中如果要注入EJB实例,常规的@Inject将不起作用,在Jboss中,应用甚至都启动不起来(因为@Inject注入失败),解决方法很简单:将@Inject换成@EJB ...

  5. SQL Server的非聚集索引中会存储NULL吗?

    原文:SQL Server的非聚集索引中会存储NULL吗? SQL Server的非聚集索引中会存储NULL吗? 这是个很有意思的问题,下面通过如下的代码,来说明,到底会不会存储NULL. --1.建 ...

  6. 非spring组件servlet、filter、interceptor中注入spring bean

    问题:在filter和interceptor中经常需要调用Spring的bean,filter也是配置在web.xml中的,请问一下这样调用的话,filter中调用Spring的某个bean,这个be ...

  7. Spring @Autowired注解用在集合上面,可以保持接口的所有实现类

    CourseService课程接口有2个子类,HistroyCourseServiceImpl和MathsCourseServiceImpl public interface CourseServic ...

  8. Spring@Autowired注解

    @Autowired注解可以对成员变量.方法和构造函数进行标注,来完成自动装配的工作. 注意:@Autowired默认是按照类型来注入的. 看下面的例子:例子是以对成员变量(field)为例进行的 p ...

  9. Spring @Autowired 注解自动注入流程是怎么样?

    面试中碰到面试官问:"Spring 注解是如果工作的?",当前我一惊,完了这不触及到我的知识误区了吗?,还好我机智,灵机一动回了句:Spring 注解的工作流程倒还没有看到,但是我 ...

随机推荐

  1. linux文件 面试知识

    1.        文件存储结构 Linux正统的文件系统(如ext2.ext3)中,一个文件由目录项.inode和数据块组成. 目录项:包括文件名和inode节点号. inode:又称文件索引节点, ...

  2. 【.Net】含Unicode的字符串截断 VB.NET C#

    Function AnsiLeftB(ByVal strArg As String, ByVal arg1 As Integer) As String Dim unicodeEncoding As E ...

  3. 不用bootstrap,只用CSS创建网格布局

    本文译自[http://j4n.co/blog/Creating-your-own-css-grid-system],英语好的,可直接查看原网页,不需要FQ. 翻译拿不准的地方会有英文原文,方便大家理 ...

  4. ABP学习入门系列(一)(第一个ABP项目)

    ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称.ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应用程序的新起点,它 ...

  5. 《码出高效 Java开发手册》第一章计算机基础(未整理)

    码云地址: https://gitee.com/forxiaoming/JavaBaseCode/tree/master/EasyCoding

  6. vue项目引入element

    前提工作-安装并配置好以下环境: 1.安装node  2.安装git 1.初始化项目 vue init webpack vue-elementui npm run dev 2.安装element np ...

  7. R语言与.net 集成开发入门

    首先:R语言的基本教程: https://www.yiibai.com/r/r_environment_setup.html 下载R语言的安装包:https://cran.r-project.org/ ...

  8. K:伸展树(splay tree)

      伸展树(Splay Tree),也叫分裂树,是一种二叉排序树,它能在O(lgN)内完成插入.查找和删除操作.在伸展树上的一般操作都基于伸展操作:假设想要对一个二叉查找树执行一系列的查找操作,为了使 ...

  9. POJ3279(KB1-D 熄灯问题)

    Fliptile Description Farmer John knows that an intellectually satisfied cow is a happy cow who will ...

  10. sql判断某个字段是否为空

    判断sql某个字段是否为NULL public function dataNull($id){ $sql = 'SELECT * FROM `vvt_company_funcs_user` WHERE ...