title: Spring Boot@Component注解下的类无法@Autowired的问题

date: 2019-06-26 08:30:03

categories:

  • Spring Boot

    tags:
  • 注入问题

这个问题心累

在把我的一个非Web程序迁移从Spring迁移到SpringBoot时,出现了在@Component注解下@Autowired的类为null的情况,也就是没注入成功,或者说是此类在bean加载之前就被调用了。

试了各种办法,修改扫描包,修改@Component注解等等,皆未成功,后来看到了一个方法,探究了一下。

@Component
public class ComponentClass {
<span class="token annotation punctuation">@Autowired</span>
<span class="token keyword">private</span> JedisClient jedisClient<span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> ComponentClass componentClass<span class="token punctuation">;</span>
<span class="token annotation punctuation">@PostConstruct</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
componentClass <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">;</span>
componentClass<span class="token punctuation">.</span>jedisClient <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>jedisClient<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

  • 声明一个此类的静态变量,用以保存bean。
  • 使用@PostConstruct注解,将需要注入的类添加到静态变量中。
  • 接下来,使用这个静态变量来调用注入类就行了。

@PostConstruct这个注解的具体作用就是:

注解在方法上,表示此方法是在Spring实例化该bean之后马上执行此方法,之后才会去实例化其他bean

这样在Spring实例化ComponentClass之后,马上执行此方法,初始化ComponentClass静态对象和成员变量jedisClient

原文地址:https://blog.csdn.net/weixin_38950807/article/details/93709887

Spring Boot@Component注解下的类无法@Autowired的问题的更多相关文章

  1. 解决Spring Boot集成Shiro,配置类使用Autowired无法注入Bean问题

    如题,最近使用spring boot集成shiro,在shiroFilter要使用数据库动态给URL赋权限的时候,发现 @Autowired 注入的bean都是null,无法注入mapper.搜了半天 ...

  2. Spring Boot常用注解总结

    Spring Boot常用注解总结 @RestController和@RequestMapping注解 @RestController注解,它继承自@Controller注解.4.0之前的版本,Spr ...

  3. Spring Boot 常用注解汇总

    一.启动注解 @SpringBootApplication @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documen ...

  4. 3个Spring Boot核心注解,你知道几个?

    Spring Boot 核心注解讲解 Spring Boot 最大的特点是无需 XML 配置文件,能自动扫描包路径装载并注入对象,并能做到根据 classpath 下的 jar 包自动配置. 所以 S ...

  5. 【SpringBoot】15. Spring Boot核心注解

    Spring Boot核心注解 1 @SpringBootApplication 代表是Spring Boot启动的类 2 @SpringBootConfiguration 通过bean对象来获取配置 ...

  6. Spring Boot中注解@ConfigurationProperties

    在Spring Boot中注解@ConfigurationProperties有三种使用场景,而通常情况下我们使用的最多的只是其中的一种场景.本篇文章带大家了解一下三种场景的使用情况. 场景一 使用@ ...

  7. spring boot 结合Redis 实现工具类

    自己整理了 spring boot 结合 Redis 的工具类引入依赖 <dependency> <groupId>org.springframework.boot</g ...

  8. Spring Boot 核心注解与配置文件

    @SpringBootApplication注解 Spring Boot项目有一个入口类 (*Application) 在这个类中有一个main 方法,是运行该项目的切入点.而@SpringBootA ...

  9. Spring boot 基于注解方式配置datasource

    Spring boot 基于注解方式配置datasource 编辑 ​ Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...

随机推荐

  1. MSSQL手工注入 报错注入方法

    例子:www.kfgtfcj.gov.cn/lzygg/Zixun_show.aspx?id=1[1]首先爆版本:http://www.kfgtfcj.gov.cn/lzygg/Zixun_show. ...

  2. Js 之正则验证手机号、QQ、身份证等

    // 常见的 正则表达式 校验 // QQ号.手机号.Email.是否是数字.去掉前后空格.是否存在中文.邮编.身份证.URL.日期格式.IP let myRegExp = { // 检查字符串是否为 ...

  3. Make sure you've included captcha.urls as explained in the INSTALLATION section on

    原因:django-simple-captcha将客户端编号与验证码默认存储在数据库中 解决办法: python manage.py migrate

  4. Android HIDL学习(2) ---- HelloWorld【转】

    本文转载自: 写在前面 程序员有个癖好,无论是学习什么新知识,都喜欢以HelloWorld作为一个简单的例子来开头,咱们也不例外. OK,咱这里都是干货,废话就不多说啦,学习HIDL呢咱们还是需要一些 ...

  5. 单细胞数据normalization方法 | SCTransform

    SCTransform Normalization and variance stabilization of single-cell RNA-seq data using regularized n ...

  6. 清理收藏夹中的json

    1.json的字符串与对象转换 $.parseJson(str)与JSON.parse(str) 返回值是true.另:json对象和字符串的相互转换 JSON.stringify(obj) 将JSO ...

  7. numpy模块常用函数解析

    https://blog.csdn.net/lm_is_dc/article/details/81098805 numpy模块以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter note ...

  8. SQL Server 变量定义

    declare @id intdeclare @name char(10) --注意:char(10)为10位,要是位数小了会让数据出错set @id=1 set @name='sssss'selec ...

  9. git切换分支冲突解决-删除分支

    在项目开发中,有多个版本分支需要不时的来回切换,在切换的过程中,产生了很多冲突,提交的时候 也提交不了.总结下在解决这个过程中使用的两种方法: 1.删除项目在磁盘的目录,包括 git 文件,重新 cl ...

  10. [转]vue项目中 指令 v-html 中使用过滤器filters功能

    转载于简书 链接:http://www.jianshu.com/p/29b7eaabd1ba 问题 2.0 filters only work in mustache tags and v-bind. ...