我想在控制器中使用buisness层方法,并且我不断得到nullPointerException。嗯,我知道这是因为我无法实现一个商务对象suince我正在处理界面。那么如何通过商务界面从控制器调用商务方法呢?这是一个示例商务界面:
public interface ICompagnieBuisness {
public String depa="";
String depString(String dep); }
这里是conroller,我想调用depString(String dep)方法返回一个String,我将把它放在视图中使用的消息中:
`package com.crun.controller;
@Controller
public class HelloWorld {
@Autowired
private static ICompagnieBuisness icb;
public void setIcb() {
this.icb = icb;
}
@RequestMapping("/welcome")
public ModelAndView helloWorld() {
String dep=icb.depString(a);
String message = "<br><div style='text-align:center;'>"
+ "<h3>***Hello World, Spring MVC Tutorial</h3> </div><br><br>";
return new ModelAndView("welcome", "message", message);
}
}`
这是商务舱: 包com.crun.buisness;
import java.util.List;
import com.crunc.dao.ICompagnieDAO;
public class ICompagnieBuisnessImpl implements ICompagnieBuisness{
private static ICompagnieDAO dao;
public String depa;
public String depString(String dep) {
dep="Message sent from buisness layer";
return dep;
}
}
这是错误
GRAVE: Servlet.service() for servlet [crun] in context with path [/CrunSpringMVCTutorial] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.crun.controller.CrunHelloWorld.helloWorld(CrunHelloWorld.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandl erMethod(HandlerMethodInvoker.java:175)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.in vokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.ha ndle(AnnotationMethodHandlerAdapter.java:434)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
非常感谢!
答案 0 :(得分:0)
为了将ICompagnieBuisness
的实例注入控制器,请确保Spring知道接口的实现(它应该是一个Spring bean)。您可以使用@Component
,@Repository
或@Service
对其进行注释,使其成为Spring bean。在您的情况下,使用@Service
在上下文中更正确,因为它是业务层。此外,您无法将spring bean自动装配到静态字段,因此请在字段声明中删除static关键字。