Search code examples
javaspringdependency-injectioninversion-of-controljavabeans

Can't get Spring to inject my dependencies


I've been trying to get Spring to inject an @Autowired dependency into my application without avail. What am I doing wrong?

I created a bean called TimeService. Its job is to return the current time to anyone that asks.

package com.foxbomb.springtest.domain.time;

import java.util.Date;
import org.springframework.stereotype.Service;

@Service
public class TimeService {

    public TimeService() {
        super();
        System.out.println("Instantiating TimeService...");
    }

    public Date getTime() {
        return new Date();
    }
}

Of course, I need to tell Spring about this, so I added the following to web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-context.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>  

Great, and some Spring configuration:

<?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"
     xsi:schemaLocation="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 base-package="com.foxbomb.springtest.domain" />

</beans>

Now all we need is a calling class that would like to use this dependency. Unfortunately, the @Autowired here seems to do nothing:

package com.foxbomb.springtest;

import...

@Configurable
public class TimeManager {

    public TimeManager() {
        super();
        System.out.println("Instantiating TimeManager...");
    }

    @Autowired
    private TimeService timeService;

    public Date getTime() {
        return timeService.getTime();
    }

}

And lastly, a JSP that wants to display the time:

<%@page import="com.foxbomb.springtest.ApplicationContextImpl"%>
<%@page import="com.foxbomb.springtest.TimeManager"%>

<html>
    <head>
        <title>Spring Test</title>
    </head>
    <body>
        <h1>Autowired Dependencies....</h1>
        <p>
            Time is now <%=new TimeManager().getTime()%>!
        </p>

    </body>
</html>

But all I get is:

java.lang.NullPointerException
    com.foxbomb.springtest.TimeManager.getTime(TimeManager.java:26)

Solution

  • Finally!

    I got it working with the code in the above example. The trick was to set up Aspect/J and Weaving. This allows the @Configurable annotated class to automatically realize that its dependent on Spring to inject dependencies.

    There wasn't much wrong with the base package :) I accidentally made a xml typo, trying to add the base-package attribute to the context:annotation-config tag. The correct definition was:

    <context:annotation-config/>
    <context:component-scan base-package="com.foxbomb.springtest"/>
    

    I need actually eventually need to change it to com.foxbomb.springtext to include the class that was trying to access the bean too - as in the end it's also annotated.

    Thanks everyone for your help!