创建名为"baseController"的bean时出错:调用init方法失败;嵌套异常为java.lang.NullPointerException位于org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroAnnotationeanPostProcessor.java:160)位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416)网址:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788)网址:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)网址:org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)位于org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)位于org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)位于org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)位于org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)位于org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)位于org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)网址:org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)位于org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:702)网址:org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:668)网址:org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:716)网址:org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:591)网址:org.springframework.web.servlet.FrameworkServlet.initServlet Bean(FrameworkServlet.java:530)网址:org.springframework.web.servlet.HttpServlet Bean.init(HttpServlet Bean.java:170)位于javax.servlet.GenericServlet.init(GenericServlet.java:158)网址:org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1143)网址:org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1090)网址:org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:984)网址:org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4891)网址:org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5202)网址:org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)网址:org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1412)网址:org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1402)位于java.util.concurrent.FFutureTask.run(FutureTask.java:266)位于java.util.concurrent.ThreadPoolExecutiator.runWorker(ThreadPoolExecutiator.java:1149)位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)在java.lang.Thread.run(线程.java:748)导致原因:java.lang.NullPointerException位于DiamondShop.Service.User.HomeServiceIml.GetDataMenus(HomeServiceIml.java:33)在DiamondShop.Controller.User.BaseController.Initive(BaseController.java:19)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)位于java.lang.reflect.Method.ioke(Method.java:498)位于org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.ioke(InitDestroAnnotationeanPostProcessor.java:389)位于org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroAnnotationeanPostProcessor.java:333)位于org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroAnnotationeanPostProcessor.java:157)…还有30个
@Controller
public class BaseController {
@Autowired
HomeServiceIml _homeService;
public ModelAndView _mvShare = new ModelAndView();
@PostConstruct
public ModelAndView Init () {
_mvShare.addObject("menus", _homeService.GetDataMenus());
return _mvShare;
}
}
@Controller
public class HomeController extends BaseController{
@RequestMapping(value = {"/","/trang-chu"} )
public ModelAndView Index() {
ModelAndView mv= new ModelAndView("user/index");
mv.addObject("slides", _homeService.GetDataSlide());
mv.addObject("categorys", _homeService.GetDataCategorys());
return mv;
}
@RequestMapping(value = "/product")
public ModelAndView Product() {
ModelAndView mv= new ModelAndView("user/product");
return mv;
}
}
用@PostConstruct注释的方法不应该有任何返回值!有关更多信息,请查看此处:https://docs.oracle.com/javase/10/docs/api/javax/annotation/PostConstruct.html
在非拦截器类上定义的方法必须具有以下签名:void()应用PostConstruct的方法可以是public、protected、package private或private。除了应用程序客户端之外,该方法不能是静态的。该方法可能是最终的。如果该方法抛出未检查的异常,则该类不得投入服务,除非EJB可以处理异常,甚至可以从中恢复。
一个典型的用例可以在这里找到:https://www.baeldung.com/spring-postconstruct-predestroy