这些项目在Tomcat服务器上运行。
项目结构:(包含以下3个项目)
共享
public interface ConfigParamDAOnew {
public static final String CLASS_NAME = "paramDAO";
String getParamValueByName(String name);
}
工具包
@Component
public class ParamModel_Global_DAO_Impl implements ConfigParamDAOnew {
@Autowired
ParamModel_GlobalJPARepository jpa;
@Autowired
ServletContext context;
@Override
public String getParamValueByName(String name) {
return jpa.findByName(name).get().getValue();
}
@PostConstruct
public void setup() {
context.setAttribute(ConfigParamDAOnew.CLASS_NAME, this);
}
}
服务
@Service
public class NewConfigParamDAO {
@Autowired
ServletContext servletContext;
ConfigParamDAOnew dao;
public NewConfigParam getParam(String name) {
loadConfigParamDAO();
return new NewConfigParam(name, dao.getParamValueByName(name));
}
private void loadConfigParamDAO() {
dao = (ConfigParamDAOnew)
servletContext.getAttribute(ConfigParamDAOnew.CLASS_NAME);
}
> Caused by: java.lang.NullPointerException at > de.usu.si.toolkit.config.NewConfigParamDAO.getParam(NewConfigParamDAO.java:35) > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at > sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) > at > sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) > at java.lang.reflect.Method.invoke(Method.java:498) at > org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:365) > at > org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:308) > at > org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:135) > ... 27 more
从工具箱项目中请求启动后,初始引导失败。这对我有用,然后才可以在哪里出错?
答案 0 :(得分:0)
发生此问题的原因是,在bean ParamModel_Global_DAO_Impl之前创建了bean NewConfigParamDAO,然后在ServletContext中找不到该bean。
我通过在NewConfigParamDAO中注释@DependsOn(“ paramModel_Global_DAO_Impl”)来解决此问题