相关文章推荐
火爆的山楂  ·  c# - How to implement ...·  2 年前    · 
坚强的机器猫  ·  Python 多进程库 ...·  2 年前    · 
善良的李子  ·  VBA 链接 SQL Server ...·  2 年前    · 
爱热闹的小马驹  ·  az sentinel ...·  2 年前    · 
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 -

How is your package set up ? Is your Service class located in a different packing structure from your Application and Repository class ? – s7vr Mar 1, 2017 at 20:25 Yes, Service class is localted in a different package. Repository and Subject classes are in db package, Service is in service package and Application class is outside. – Akshay Megharaj Mar 1, 2017 at 22:36 You can annotate your Service with @Service annotation and if the Application class is at the root level package then your annotation should be picked up. – s7vr Mar 1, 2017 at 22:45

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 "";
                Tried all the three annotations individually, none of them worked. Added all the annotations that I have for Application class, even this did not work.
– Akshay Megharaj
                Mar 1, 2017 at 22:45
        

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.