Michael Melsen
Michael Melsen

Reputation: 11

spring autowiring fails but appcontext resolves bean

i have the following issue with spring. I have a webapp and a domain project. the domain project contains a studentService which should be injected through autowiring in a class of the webapp. I've added and to the appcontext.xml.

this is the class from the webapp:

@Component
public class JSONToDomainObjects
{

@Autowired
private StudentService studentService;

private void bindSubmissionValuesToDomainObjects(Integer userKey) throws Exception
{
 Student student =  studentService.getStudentBySlNumber(userKey);
}
}

then the studentservice:

@Service
public class StudentService
{
  ..
}

So once I startup my app I see that the studentService is null, but when I get the appcontext and invoke the method getBean("studentService") than a studentservice instance is returned. I use spring 3.0.5. Does anybody have a clue why the autowiring fails?

cheers,

Michael

Upvotes: 1

Views: 946

Answers (2)

abalogh
abalogh

Reputation: 8281

Why don't you use dependency injection in your testclasses as well? Something like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"appcontext.xml"})
public final class JSONToDomainObjectsTests {
    private StudentService service;

    @Autowired
    public void setService(StudentService service) {
        this.service= service;
    }

    @Test
    public void testJSONToDomain() {
        service.foo();
    }
}

Upvotes: 2

abalogh
abalogh

Reputation: 8281

Are you using <context:annotation-config/> in your appcontext.xml?

Upvotes: 1

Related Questions