I'm trying to write an access layer class that gets data from MongoDB using Spring Data, but I have the following problem: I have the following interface:
public interface BlogDataRepository extends MongoRepository<Article, String> {
public Article findArticleByName(String name);
}
and the access layer class:
@EnableAutoConfiguration
public class BlogDataAccessLayer {
@Autowired
private BlogDataRepository dataRepository;
...
}
And finally a main class:
@EnableAutoConfiguration
public class Test implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(Test.class, args);
}
public void run(String... args) throws Exception {
BlogDataAccessLayer layer = new BlogDataAccessLayer();
Article article = new Article("test", "first article");
layer.addArticle(article);
}
}
Each time I'm trying to run the app I get a NullPointerExeption
from dataRepository
located in BlogDataAccessLayer
.
I don't understand why dataRepository
is not autowired. If I move dataRepository
in Test
it works.
Can somebody explain why I get this behavior?