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

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

该注解若是在容器 tomcat 启动后引用后不会报错,不会报 java.lang.NullPointerException 错误。数据库

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

解决办法: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注解的类实体化this