[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!