Total Pageviews

2011/09/01

@Autowire is not working properly in my spring application

Problem
From my @Controller, i can easily access my autowired @Service class and play with it no problem. But when I do that from a separate class without annotations, it gives me a NullPointerException.


Solution
Spring dependency injection works only in components managed by Spring.


By default, classes annotated with @Component, @Repository, @Service, @Controller, or a custom annotation that itself is annotated with @Component are the only detected candidate components.


If your class is not managed by Spring (i.e. is not a Spring bean), @Autowired doesn't work inside it.

No comments: