gpt4 book ai didi

java - Spring bean 注入(inject)无法正常工作

转载 作者:行者123 更新时间:2023-12-01 11:10:09 24 4
gpt4 key购买 nike

我正在尝试在 Maven 3.3.3 中创建一个 Spring + jersey 项目。 war 部署成功。但在 Rest 服务类中,bean 注入(inject)不起作用。我正在使用 setter 注入(inject)。我收到错误

Sep 09, 2015 1:49:23 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.NullPointerException
at com.marvel.scheduler.services.rest.impl.SchedulerWebServiceImpl.authenticate(SchedulerWebServiceImpl.java:308)
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 com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
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:504)
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:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
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)

我的 web.xml 是

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html#d4e194 -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>Marvel Health Scheduler</display-name>
<description>Appointments Scheduler Software for Marvel-Health</description>
<!--
- Key of the system property that should specify the root directory of this
- web app. Applied by WebAppRootListener or Log4jConfigListener.
-->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>aFSchedulerMaven.root</param-value>
</context-param>
<!--
- Location of the Log4J config file, for initialization and refresh checks.
- Applied by Log4jConfigListener.
-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>


<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/marvemr-servlet.xml /WEB-INF/classes/dataAccessContext-local.xml /WEB-INF/classes/dataAccessContext-Extra.xml
</param-value>
</context-param>

<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>


<servlet>
<servlet-name>JerseyRESTServlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.marv.scheduler.services.rest.impl</param-value>
</init-param>

<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.marv.scheduler.services.rest.responsefilter.ResponseCorsFilter</param-value>
</init-param>

<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>

<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JerseyRESTServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>60</session-timeout>
</session-config>

<welcome-file-list>
<!-- Redirects to "welcome.htm" for dispatcher handling -->
<welcome-file>index.html</welcome-file>
</welcome-file-list>

<error-page>
<error-code>404</error-code>
<location>/errorpage.html</location>
</error-page>

我的其余服务层类是:

@Path("schedulerservices")
@Produces(MediaType.APPLICATION_JSON)
public class SchedulerWebServiceImpl{
private ErrorMessage errorMessage;
private Logger logger;
private SchedulerService schedulerService;
private CommonService commonService;
private PerformanceLogger performanceLogger;

public void setErrorMessage(ErrorMessage errorMessage) {
this.errorMessage = errorMessage;
}

public void setLogger(Logger logger) {
this.logger = logger;
}

public void setAdminService(SchedulerService schedulerService) {
this.schedulerService = schedulerService;
}

public void setCommonService(CommonService commonService) {
this.commonService = commonService;
}

public void setPerformanceLogger(PerformanceLogger performanceLogger) {
this.performanceLogger = performanceLogger;
}

我的pom.xml是

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-support</artifactId>
<version>2.0.6</version>
</dependency>

我的bean注入(inject)xml是:

<bean id="restRootWebService" class="com.marvel.scheduler.services.rest.impl.SchedulerWebServiceImpl" >
<property name="errorMessage"><ref bean="errorMessage" /></property>
<property name="logger"><ref bean="logger" /></property>
<property name="schedulerService"><ref bean="adminService" /></property>
<property name="commonService"><ref bean="commonService" /></property>
<property name="performanceLogger"><ref bean="performanceLogger" /></property>
</bean>

请帮我找出解决方案...

  @POST 
@Path("login")
@Consumes(MediaType.APPLICATION_JSON)
public Response authenticate(AuthenticateInputParam authenticateInputParam) throws java.rmi.RemoteException {
AuthenticateOutput authenticateOutput = schedulerService.authenticate(authenticateInputParam); // this is the code 308
if (authenticateOutput !=null)
return Response.ok(authenticateOutput).status(200).build();
}

最佳答案

您是否考虑过构造函数注入(inject)而不是 setter 注入(inject)?这使得滥用您的服务变得更加困难,因为在提供所有依赖项之前您无法拥有实例。

private final ErrorMessage errorMessage;
private final Logger logger;
private final SchedulerService schedulerService;
private final CommonService commonService;
private final PerformanceLogger performanceLogger;

public SchedulerWebServiceImpl(
ErrorMessage errorMessage,
Logger logger,
SchedulerService schedulerService,
CommonService commonService,
PerformanceLogger performanceLogger
) {
// set all final fields
}

关于java - Spring bean 注入(inject)无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32475909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com