Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Many similar questions like this, but none of the solution has worked for me yet. I have a Repository interface that extends MondoRepository.
public interface Repository extends MongoRepository<Subject, String> {
Subject findByName(String name);
And this is the subject class
public class Subject {
private String id;
private String name;
public String getName() {
return this.name;
The upper two files are in /db package
If I autowire Repository in my Application class, I'm able to use it properly.
@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@EnableMongoRepositories(basePackageClasses=Repository.class )
public class Application implements CommandLineRunner{
@Bean
public Service service() {
return new Service();
@Autowired
private Repository repository;
@Override
public void run(String... args) throws Exception {
repository.save(new Subject());
Subject subject = repository.findByName("hello");
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
My Application class is in the root folder / .
Basically, this gives me no error. But if I autowire this in a Service class like this
public class Service {
@Autowired
private Repository repository;
public String get() {
repository.findByName("hello");
return "";
This gives me a null pointer exception in the line repository.findByName() .
My Service class is in /service package .
Why does it not behave same like in the application class?
What should I do differently to be able to use repository in the service class?
What I have tried -
I tried annotating my Service class with @Service, @Repository, @Component, none of them worked.
I tried adding annonation @ComponentScan(basePackageClasses = Service.name) for Application class, it didn't work .
Any workaround is also fine -
–
–
–
Maybe it's because the Repository or Service hasn't been scanned by spring? have you annotated as @service, @repository or @component?
these works for me:
@SpringBootApplication
@EnableMongoRepositories
public class DemoApplication implements CommandLineRunner{
@Autowired
private Service service;
@Override
public void run(String... args) throws Exception {
String subject = service.get();
System.out.println("Hola" + subject);
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
@org.springframework.stereotype.Service
public class Service {
@Autowired
private Repository repository;
public String get() {
repository.findByName("hello");
return "";
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.