I am using Jersey 2.7 and Spring along with Hibernate. I get a null pointer exception for a @Autowired bean declared in my applicationContext.xml
My web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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>CWS2020POC</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>com.cws.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>RESTService</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.cws.rest</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>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RESTService</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Below is my applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config />
<context:component-scan base-package="com.cws.rest" />
<context:component-scan base-package="com.cws.dao" />
<bean id="caseParticipantsDAO" class="com.cws.dao.impl.CaseParticipantsDAOImpl" />
</beans>
I have autowired the bean caseParticipantsDAO as follows
@Path("/caseParticipantsService")
@Component
public class CaseParticipantsService {
@Autowired
private CaseParticipantsDAO caseParticipantsDAO;
@SuppressWarnings({})
@Path("/getCaseParticipants/{caseId}/")
@GET
@Produces(MediaType.APPLICATION_JSON)
public CaseParticipants getCaseClients(@PathParam("caseId") int caseId) {
return caseParticipantsDAO.getCaseClients(caseId);
}
}
I get a null-pointer at this line
return caseParticipantsDAO.getCaseClients(caseId);
where caseParticipantsDAO is null.
If I manually try to load the applicatioContext.xml using the following code
ApplicationContext configFile = new ClassPathXmlApplicationContext(
"applicationContext.xml");
CaseParticipantsDAO caseParticipantsDAO = (CaseParticipantsDAO)
configFile
.getBean("caseParticipantsDAO");
Everything works as expected. But i needd to load the applicationContext.xml through web.xml and want to persist the context throughout the application so i can autowire my beans as and when needed.