最近用spring boot ,在controller外面即自定义的类里报错

java.lang.NullPointerException

debug了下发现@Resource注入为null

查了不少资料也没解决问题,最后终于找到一种解决办法,为了以后查找方便特意记录下来

@Component
public class SaveStructures {
private static SaveStructures structures;
@Resource
private ApiFilesService filesService;
@Resource
private ApiAssetsMapper assetsMapper; public SaveStructures() {
} //通过@PostConstruct实现初始化bean之前进行的操作
@PostConstruct
public void init() {
// 初使化时将已静态化的Service实例化
structures = this;
} ……
……
structures.assetsMapper.batchAddAssets(list);
……
structures.filesService.findByCondition(condition); }

注:

  注入类的调用方法

    structures.filesService.findByCondition(condition);

说明:

  被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。

  被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行

springboot自定义类@Resource注入为null的问题的更多相关文章

  1. Spring_关于@Resource注入为null解决办法

    初学spring,我在dao层初始化c3p0的时候,使用@Resource注解新建对象是发现注入为null,告诉我 java.lang.NullPointerException. @Repositor ...

  2. 源码分析springboot自定义jackson序列化,默认null值个性化处理返回值

    最近项目要实现一种需求,对于后端返回给前端的json格式的一种规范,不允许缺少字段和字段值都为null,所以琢磨了一下如何进行将springboot的Jackson序列化自定义一下,先看看如何实现,再 ...

  3. @Resource注入为null

    UserController中userService为null只有一个原因, 那就是你所使用的UserController对象没有被IoC容器所管理,你使用的对象是自己new出来的, 而不由sprin ...

  4. Springboot 工具类静态注入

    用springboot搭了一个项目,里面要用到一个DictUtils,因为要用到DictMapper,在百度找了一些方法,最后用下面的方法能成功获取到DictMapper @Component pub ...

  5. 关于工具类中@Autowired注入为NULL的问题记录

      记录:在实体类中加入@Component注解和@Autowired注解时Service不能注入成功. @Component //把普通pojo实例化到spring容器中 0 public clas ...

  6. java-普通类文件@Autowired自动注入为null

    @Autowired注解在非Controller中注入为null 1.配置文件(类文件所在的包) <context:component-scan base-package="net.n ...

  7. 于工具类中@Autowired注入为NULL的问题记录

      记录:在实体类中加入@Component注解和@Autowired注解时Service不能注入成功. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...

  8. springboot管理类,springboot注入类

    springboot管理类,springboot注入类 定义一个配置类,添加@Configuration注解,EvaluatorTemplate代表你需要注入的第三方类 @Configuration ...

  9. 在Springmvc普通类@Autowired注入request为null解决方法

    在Springmvc普通类@Autowired注入request为null解决方法   在类中加入以下注入request对象的代码,运行时发现request为null,注入失败.在@Controlle ...

随机推荐

  1. query_posts函数使用方法小结|wordpress技巧

    query_posts是wordpress非常好用的调用文章函数,可以调用某个分类.标签.日期及作者等不同范围的文章列表.下面随ytkah一起来看看query_posts函数使用方法小结 首先是que ...

  2. hook杂思-面向函数编程

    hook:方法拦截 以函数单元为编程对象: 在编译时或运行时进行函数单元的替代.修改.功能添加操作: 所有的操作都不是在原始编码时完成的: 函数单元作为参量.操作对象.编码对象存在于机制中: 机制: ...

  3. 代码的结合性:继承 扩展 组合 变换--swift暗含的四根主线

    类型继承: 类型扩展: 类型组合: 类型变换:

  4. node.js – 服务器端的客户端证书验证,DEPTH_ZERO_SELF_SIGNED_CERT错误

    我正在使用节点0.10.26并尝试建立与客户端验证的https连接. 服务器代码: var https = require('https'); var fs = require('fs'); proc ...

  5. Laravel 解决blade模板转义html标签问题

    当我们使用富文本编译器(如:Ueditor编译器)保存编辑的内容后,在blade模板中,想要显示原生的html标签内容时该怎么做? 首先,了解下laravel {{ 变量名 }} 与{!! 变量名 ! ...

  6. 可持久化0-1Trie树

    我跟可持久化数据结构杠上了 \(QwQ\) .三天模拟赛考了两次可持久化数据结构(主席树.可持久化0-1Trie树),woc. 目录: 个人理解 时空复杂度分析 例题及简析 一.个人理解 可持久化0- ...

  7. Spring事务经典案例-银行转账

    1.entity实体类 2.dao层 3.dao实现类 4.service层 5.serviceimpl层 6.大配置.xml <?xml version="1.0" enc ...

  8. pytesseract.pytesseract.TesseractError: (1, 'Error opening data file /usr/local/share/tessdata/chi_sim.traineddata Please make sure the TESSDATA_PREFIX environment variable is set to your "tessdata"

    pytesseract.pytesseract.TesseractError: (1, 'Error opening data file /usr/local/share/tessdata/chi_s ...

  9. pytest以函数形式形成测试用例

    #coding=utf- from __future__ import print_function #开始执行该文件时,该函数执行 def setup_module(module): print(' ...

  10. ~/.ssh/config文件的使用

    Host github-A HostName github.com User git IdentityFile /Users/xxx/.ssh/id_rsa_A IdentitiesOnly yes ...