码迷,mamicode.com
首页 > 编程语言 > 详细

Failed to instantiate [引用 spring 配置文件的类]: Constructor threw exception; nested exception is java.lang.NullPointerException

时间:2018-01-13 15:43:32      阅读:8996      评论:0      收藏:0      [点我收藏+]

标签:hashmap   int   gpo   xtend   nested   autowired   const   stc   lang   

本人的继承类引用了 service 层的方法,用的注解方式是@Autowired

该注解如果在容器 tomcat 启动后引用后不会报错,不会报 java.lang.NullPointerException 错误。

初始化时如果想调用数据库的数据,而此时 service 层的类没有实例化,导致 tomcat 启动时报空指针错误。

解决办法:

@Component
public class MessageResource extends AbstractMessageSource implements ResourceLoaderAware
{
@Autowired
private TsLanguageTestService tsLanguageTestService;

private static MessageResource util;

/**
* Map切分字符
*/
protected final String MAP_SPLIT_CODE = "|";
private final Map<String, String> properties = new HashMap<String, String>();

/**
* 初始化数据,把数据库里的国际化都读入缓存的Map里
*/
@PostConstruct
public void init() {
util = this;
util.tsLanguageTestService = this.tsLanguageTestService;
}
}

主要是 init() 方法和@PostConstruct 注解,这样 tomcat 启动时就会把@Autowired注解的类实体化


 

Failed to instantiate [引用 spring 配置文件的类]: Constructor threw exception; nested exception is java.lang.NullPointerException

标签:hashmap   int   gpo   xtend   nested   autowired   const   stc   lang   

原文地址:https://www.cnblogs.com/silense/p/8278957.html

(1)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!