自动装配的bean为null,但它在Spring容器中有价值[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

[已解决]在这种情况下,问题是我没有在构建路径中包含一些有用的库,例如“jersey-spring4-2.28.jar”和“spring-bridge-2.5.0.jar”。这些库允许Jersey和Spring之间的集成。

我正在开发一个关于Tomcat(9.0.16)的Web应用程序,它为数据通信提供了有用的REST服务(在Jersey 2.28的帮助下提供的服务)。在管理这些服务的类中,我使用Spring(v5.1.5)注入依赖项,如下所示:

package it.learning.rest;

@Component
@Path("/")
public class GameClientCommsRestService {

@Autowired
@Qualifier("CommunicationBusiness")
private GameClientCommunication comm;

@GET
@Path("/test/check/bean")
@Produces(MediaType.TEXT_PLAIN)
public Response testCheckCommsBean() {
  StringBuilder sb = new StringBuilder();
  if (comm == null) {
    sb.append("GameClientCommunication instance is NULL!!");
  } else {
    sb.append("GameClientCommunication instance is NOT NULL ---> SUCCESSFULLY instantiated");
  }

  return getRestResponse(sb.toString());
}

}

注入依赖项的类定义如下:

package it.learning.business.impl;

public class GameClientCommunicationBusiness implements GameClientCommunication {

  @Override
  public String processesMessage(String request, String remoteIpAddress) {
    // Processing input...
  }

}

XML配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

  <context:component-scan base-package="it.learning" />
  <context:annotation-config />

  <bean id="applicationContextProvider" class="it.learning.utils.spring.ApplicationContextProvider">
  </bean>

  <bean id="CommunicationBusiness" class="it.learning.business.impl.GameClientCommunicationBusiness">
  </bean>

</beans>

在应用程序启动时,Tomcat和日志服务都不会记录任何问题,但是如果我打电话的话

testCheckCommsBean()

它返回

“GameClientCommunication实例为NULL!”

相反,如果我使用ApplicationContext对象来获取与“CommunicationBusiness”相关联的实例,我会得到一个正常运行的对象。

该日志显示已注释的bean和XML文件中声明的bean已成功创建并插入到Spring容器中:

2019-03-19 13:06:00,343 DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - 从ServletContext资源加载7个bean定义[/WEB-INF/spring/appContext.xml] 2019-03-19 13:06 :00,362 DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - 从ServletContext资源加载4个bean定义[/WEB-INF/spring/appContextBeans.xml] 2019-03-19 13:06:00,438 DEBUG org.springframework.beans .factory.support.DefaultListableBeanFactory - 创建单例bean的共享实例'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'2019-03-19 13:06:00,526 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - 创建共享实例of singleton bean'org.springframework.context.event.internalEventListenerProcessor'2019-03-19 13:06:00,530 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - 创建单例bean'org.springframework.context的共享实例。 event.internalEventLi stenerFactory'2019-03-19 13:06:00,532 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - 创建单例bean的共享实例'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'2011-03-19 13:06 :00,533 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - 创建单例bean的共享实例'org.springframework.context.annotation.internalCommonAnnotationProcessor'2019-03-19 13:06:00,537 DEBUG org.springframework.beans.factory .support.DefaultListableBeanFactory - 创建单例bean的共享实例'org.springframework.context.annotation.internalPersistenceAnnotationProcessor'2019-03-19 13:06:00,549 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - 创建单例的共享实例bean'fameClientCommsRestService'2019-03-19 13:06:00,579 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - 创建单例bean'的共享实例'Co mmunicationBusiness'2019-03-19 13:06:00,582 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - 创建单例bean'applicationContextProvider'的共享实例

Why is my Spring @Autowired field null?中报告的不同,我从未使用“new”关键字实例化“GameClientCommunication”对象,因此我真的不明白为什么用@Autowired注释的字段为空。

感谢您的支持!

java spring nullpointerexception null autowired
1个回答
-1
投票

您可以在GameClientCommunicationBusiness类之上尝试@component注释。

问题是你没有连接实现类。

© www.soinside.com 2019 - 2024. All rights reserved.