title: Autowire Application Context
body: <p>I am attempting to autowire a <code>WebApplicationContext</code> into a class <code>ImageCreatorUtil</code> that I have created, within my Spring MVC project.  Upon execution of a method in the class, which utilizes the application context, I always receive an NPE.  It is important to note that this method is called by an <code>ApplicationListener</code> defined in another config file.  I am not sure why the autowiring is not working.  Can anyone provide any suggestions?</p>
  
  <p><strong>servlet-context.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
          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-3.1.xsd"&gt;
  
      &lt;!-- DispatcherServlet Context: defines this servlet's request-processing 
          infrastructure --&gt;
  
      &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
      &lt;mvc:annotation-driven&gt;
          &lt;mvc:argument-resolvers&gt;
              &lt;bean class="org.springframework.data.web.PageableArgumentResolver" /&gt;
          &lt;/mvc:argument-resolvers&gt;
      &lt;/mvc:annotation-driven&gt;
  
      &lt;!-- Intercept request to blog to add paging params --&gt;
      &lt;mvc:interceptors&gt;
          &lt;mvc:interceptor&gt;
              &lt;mvc:mapping path="/blog/**"/&gt;
              &lt;bean class="org.tothought.spring.interceptors.PageableRequestHandlerInterceptor" /&gt;
          &lt;/mvc:interceptor&gt;
          &lt;mvc:interceptor&gt;
              &lt;mvc:mapping path="/**"/&gt;
              &lt;bean class="org.tothought.spring.interceptors.LookupHandlerInterceptor" /&gt;
          &lt;/mvc:interceptor&gt;
      &lt;/mvc:interceptors&gt;
  
      &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving 
          up static resources in the ${webappRoot}/resources directory --&gt;
      &lt;mvc:resources location="/resources/" mapping="/resources/**" /&gt;
  
      &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources 
          in the /WEB-INF/views directory --&gt;
      &lt;bean
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix" value="/WEB-INF/views/" /&gt;
          &lt;property name="suffix" value=".jsp" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;
          &lt;property name="maxUploadSize" value="5000000"/&gt;
      &lt;/bean&gt;
  
      &lt;context:component-scan base-package="org.tothought.spring.**" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>ImageCreatorUtil.java</strong></p>
  
  <pre><code>@Component
  public class ImageCreatorUtil {
  
      static Logger logger = LoggerFactory.getLogger(ImageCreatorUtil.class);
      static final String IMAGE_PATH_FRAGMENT = "/resources/images/resume/skills/uploaded-icons/";
  
      @Autowired
      private WebApplicationContext context;
  
      /**
       * Creates the provided file in the resources directory for access by the
       * web application.
       * 
       * @param appContext
       * @param image
       */
      public void storeImage(Image image) {
          if (image != null) {
              String realPath = ((WebApplicationContext)context).getServletContext().getRealPath("/");
              File tmpFile = new File(realPath + IMAGE_PATH_FRAGMENT + image.getName());
  
              try {
                  logger.info("Saving image to :" + tmpFile.getAbsolutePath());
                  FileUtils.writeByteArrayToFile(tmpFile, image.getFile());
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  
  }
  </code></pre>
  
  <p><strong>application-context.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt;
  
      &lt;context:property-placeholder location="classpath:META-INF/db/db.properties" /&gt;
      &lt;context:annotation-config/&gt;
  
      &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/to_thought" /&gt;
          &lt;property name="username" value="${db.user}" /&gt;
          &lt;property name="password" value="${db.password}" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="persistenceUnitName" value="toThought" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;jpa:repositories base-package="org.tothought.repositories"/&gt;
  
      &lt;!-- Application Listeners --&gt;
      &lt;bean id="lookupLoader" class="org.tothought.spring.listeners.LookupLoaderApplicationListener" /&gt; 
      &lt;bean id="imageLoader" class="org.tothought.spring.listeners.ImageLoaderApplicationListener" /&gt; 
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>Console Error</strong></p>
  
  <pre><code>SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
  java.lang.NullPointerException
      at org.tothought.spring.utilities.ImageCreatorUtil.storeImage(ImageCreatorUtil.java:34)
      at org.tothought.spring.listeners.ImageLoaderApplicationListener.onApplicationEvent(ImageLoaderApplicationListener.java:29)
      at org.tothought.spring.listeners.ImageLoaderApplicationListener.onApplicationEvent(ImageLoaderApplicationListener.java:1)
      at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97)
      at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:324)
  </code></pre>