为什么我的loginService为null?

我有这样的LoginService课:

@Service("loginService")

public class LoginService {

public boolean isValidUser(CustomerDTO customerDTO){

if(customerDTO.getUsername().equals("admin") &&

customerDTO.getPassword().equals("admin")){

return true;

}

return false;

}

}

LoginAction像这样的课:

@Controller

public class LoginAction{

private CustomerDTO customerDTO;

@Autowired

private LoginService loginService;

public CustomerDTO getCustomerDTO() {

return customerDTO;

}

public void setCustomerDTO(CustomerDTO customerDTO) {

this.customerDTO = customerDTO;

}

public String execute(){

if(loginService.isValidUser(this.getCustomerDTO())){

return "success";

}

return "failure";

}

}

当我尝试isValidUserLoginAction类中调用方法时,出现空指针异常。那是因为我的loginService对象是null。我究竟做错了什么?

这是我的异常堆栈:

java.lang.NullPointerException 

com.sc.web.LoginAction.execute(LoginAction.java:26)

sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

java.lang.reflect.Method.invoke(Method.java:483)

com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)

com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)

org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167)

com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)

org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)

com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)

com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:249)

com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)

com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:139)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)

com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)

org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:562)

org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)

org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)

org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)

org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)

org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)

org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)

org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)

org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)

org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)

org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)

org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)

org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)

org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)

java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

java.lang.Thread.run(Thread.java:745)

回答:

从堆栈跟踪中可以看到您没有使用struts-spring集成。通过spring插件将Struts和spring集成在一起。默认情况下,Struts按名称自动装配。

Spring插件通过重写Struts

ObjectFactory来增强核心框架对象的创建。创建对象时,它使用Struts配置中的class属性对应于Spring配置中的id属性。如果找不到,该类将尝试照常创建,然后由Spring自动装配。对于Action,Spring

2的bean作用域功能可用于将Action实例作用于会话,应用程序或自定义作用域,从而在默认的按请求范围之外提供高级自定义。

要使用插件,只需将其添加到类路径中即可。

要启用Spring集成,只需在应用程序中包含struts2-spring-plugin-xxx.jar。

以上是 为什么我的loginService为null? 的全部内容, 来源链接: utcz.com/qa/414797.html

回到顶部