0

I have just created a spring mvc project, there is nothing inside it except web.xml, spring-servlet.xml and ListnerClass.java.
I want to initialize system property during server startup. For this I created a servlet context

public class ListnerClass implements ServletContextListener{ @Autowired private DataSource dataSource; @Override public void contextDestroyed(ServletContextEvent arg0) { } @Override public void contextInitialized(ServletContextEvent arg0) { /* code for initializing system properties */ Connection conn=dataSource.getConnection(); } 

I get NullPointerExceptin at this line Connection conn=dataSource.getConnection();

web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring-servlet.xml </param-value> </context-param> <listener> <listener-class>com.infocentercache.manager.ListnerClass</listener-class> </listener> </webapp> 

spring-servlet.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/infocenter" /> <property name="username" value="root" /> <property name="password" value="gaurav" /> </bean> </beans> 
1
  • Not sure, but a context listener will be initialized even before spring runtime comes to life. Therefore auto wiring may not work. The datasource instance will not be initialized automatically. You should you JNDI api directly to lookup datasource in context initialized method Commented Jan 6, 2014 at 16:41

1 Answer 1

3

A ServletContextListener object registered in a deployment descriptor is not managed by Spring, it is managed by the Servlet Container. As such, Spring has no business injecting any bean into it.

The rule of thumb is, if you have an @Autowired field target and it is null, Spring was not involved. Spring will throw all sorts of exceptions if it can't resolve the @Autowired target.

There are workarounds:

Sign up to request clarification or add additional context in comments.

1 Comment

yeap! ServletContextListener is not a "component"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.