0

[RESOLVED] In this case the problem was that I haven't included into the build path some useful libs such as "jersey-spring4-2.28.jar" and "spring-bridge-2.5.0.jar". These libraries allow integration between Jersey and Spring.

I'm working on a web-app on Tomcat (9.0.16), which exposes useful REST services for data communication (services made with the help of Jersey 2.28). In the class that manages these services I used Spring (v5.1.5) to inject the dependencies as follows:

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());
}

}

The class for which the dependency is injected is defined as follows:

package it.learning.business.impl;

public class GameClientCommunicationBusiness implements GameClientCommunication {

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

}

The XML configuration file is the following:

<?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>

On application start-up, neither Tomcat nor the log service records any problems, but if I call

testCheckCommsBean()

it returns

"GameClientCommunication instance is NULL!!"

If instead I use the ApplicationContext object to get the instance associated with "CommunicationBusiness", I get a properly functioning object.

The log shows that both the annotated beans and those declared in the XML file were successfully created and inserted into the Spring container:

2019-03-19 13:06:00,343 DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 7 bean definitions from ServletContext resource [/WEB-INF/spring/appContext.xml] 2019-03-19 13:06:00,362 DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 4 bean definitions from ServletContext resource [/WEB-INF/spring/appContextBeans.xml] 2019-03-19 13:06:00,438 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' 2019-03-19 13:06:00,526 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor' 2019-03-19 13:06:00,530 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory' 2019-03-19 13:06:00,532 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' 2019-03-19 13:06:00,533 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' 2019-03-19 13:06:00,537 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor' 2019-03-19 13:06:00,549 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'gameClientCommsRestService' 2019-03-19 13:06:00,579 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'CommunicationBusiness' 2019-03-19 13:06:00,582 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'applicationContextProvider'

Differently from what reported in Why is my Spring @Autowired field null? I never instantiated a "GameClientCommunication" object using the "new" keyword, therefore I really don't understand why the field annotated with @Autowired is null.

Thanks for your support!

Pan88
  • 11
  • 4
  • 1
    The main issue addressed in the linked post is the fact that it isn't a spring managed bean and that applies here as well. Jersey is managed the `@Path` annotated bean an not spring. You actually have 2 instances, 1 spring managed and 1 jersesy managed. – M. Deinum Mar 19 '19 at 13:30
  • Thanks for your time! I was refactoring an old project that uses Java 6, Spring 3.2.8 and Jersey 1.19 and I didn't edit the code but only the libs references. With those configurations the bean was not null so that's why I asked here – Pan88 Mar 19 '19 at 13:55

1 Answers1

-1

You can try @component annotation on top of GameClientCommunicationBusiness class.

The problem is you didn't wire the implementation class.

jeff porter
  • 6,560
  • 13
  • 65
  • 123
sri ram
  • 1
  • 1
  • If I use @Component there, Spring container reports 2 instances of the same bean (one named "CommunicationBusiness" and one named "gameClientCommunicationBusiness"). So when I call the REST service I get the exception org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type... – Pan88 Mar 19 '19 at 13:11