16

I am following this tutorial ( https://www.youtube.com/watch?v=Hu-cyytqfp8 ) and trying to connect to MongoDB on a remote server in Spring Boot. I get the following message when I run the application.

Description: Parameter 0 of constructor in com.mongotest.demo.Seeder required a bean of type 'com.mongotest.repositories.StudentRepository' that could not be found.

Action: Consider defining a bean of type 'com.mongotest.repositories.StudentRepository' in your configuration.

The project structure.

enter image description here

And here are my classes

    @Document(collection = "Students")
    public class Student {

        @Id
        private String number;
        private String name;
        @Indexed(direction = IndexDirection.ASCENDING)
        private int classNo;

    //Constructor and getters and setters.
    }

    ================================

    @Repository
    public interface StudentRepository extends MongoRepository<Student, String>{

    }

    ================================

    @Component
    @ComponentScan({"com.mongotest.repositories"})
    public class Seeder implements CommandLineRunner{

        private StudentRepository studentRepo;

        public Seeder(StudentRepository studentRepo) {
            super();
            this.studentRepo = studentRepo;
        }

        @Override
        public void run(String... args) throws Exception {
            // TODO Auto-generated method stub

            Student s1 = new Student("1","Tom",1);
            Student s2 = new Student("2","Jerry",1);
            Student s3 = new Student("3","Kat",2);
            studentRepo.deleteAll();
            studentRepo.save(Arrays.asList(s1,s2,s3));

        }

    }

    ================================

    @SpringBootApplication
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }

pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>com.mongotest</groupId>
        <artifactId>demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>

        <name>demo</name>
        <description>Demo project for Spring Boot</description>

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>

        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>

        <dependencies>
            <dependency>
                <groupId>org.mongodb</groupId>
                <artifactId>mongo-java-driver</artifactId>
                <version>3.4.2</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-mongodb</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>


    </project>
Pavindu
  • 2,684
  • 6
  • 44
  • 77
Chase
  • 195
  • 1
  • 1
  • 10
  • see https://stackoverflow.com/a/45012168/3344829, can you try spring boot version `1.5.1` or less – Saravana Jan 13 '18 at 13:40
  • I tried 1.5.1.RELEASE and 1.5.0.RELEASE... still not working – Chase Jan 13 '18 at 13:57
  • 1
    we need to activate mongo repositories using `@EnableMongoRepositories`, please see the answer – Saravana Jan 13 '18 at 14:16
  • Try annotating the StudentRepository class with `@Service`. I saw this on another [answer](https://stackoverflow.com/questions/59993124/the-injection-point-has-the-following-annotations-org-springframework-beans/59994805#59994805), and this worked for me. – Lycanthropeus Apr 14 '21 at 09:24
  • @Lycanthropeus a Repository is not a Service, maybe it should be annotated as `@Repository`? – Zap Aug 20 '21 at 11:42

6 Answers6

21

Please add below annotations in DemoApplication

@SpringBootApplication
@ComponentScan("com.mongotest") //to scan packages mentioned
@EnableMongoRepositories("com.mongotest") //to activate MongoDB repositories
public class DemoApplication { ... }
Saravana
  • 12,647
  • 2
  • 39
  • 57
3

If you wish to avoid writing annotations you can simply change your packages com.mongotest.entities to com.mongotest.demo.entities and com.mongotest.repositories to com.mongotest.demo.repositories

Spring Boot architecture will take care of rest. Actually other files and packages are supposed to be either at same level or below your DemoApplication.java.

Sampada
  • 2,931
  • 7
  • 27
  • 39
Ankit Kumar
  • 231
  • 2
  • 6
3

In my case , I was getting same error using mysql db

solved using @EnableJpaRepositories

@SpringBootApplication
@ComponentScan("com.example.repositories")//to scan repository files
@EntityScan("com.example.entities")
@EnableJpaRepositories("com.example.repositories")
public class EmployeeApplication implements CommandLineRunner{ ..}
Saurabh
  • 7,525
  • 4
  • 45
  • 46
0

I have 3 helper classes RedisManager, JWTTokenCreator & JWTTokenReader which I needed to pass in constructor as dependency of SessionService spring service.

@SpringBootApplication
@Configuration
public class AuthenticationServiceApplication {
  @Bean
  public SessionService sessionService(RedisManager redisManager, JWTTokenCreator tokenCreator, JWTTokenReader tokenReader) {
    return new SessionService(redisManager,tokenCreator,tokenReader);
  }

  @Bean
  public RedisManager redisManager() {
    return new RedisManager();
  }

  @Bean
  public JWTTokenCreator tokenCreator() {
    return new JWTTokenCreator();
  }

  @Bean
  public JWTTokenReader JWTTokenReader() {
    return new JWTTokenReader();
  }

  public static void main(String[] args) {
     SpringApplication.run(AuthenticationServiceApplication.class, args);
  }
}

The service class is as follows

@Service
@Component
public class SessionService {
  @Autowired
  public SessionService(RedisManager redisManager, JWTTokenCreator 
      tokenCreator, JWTTokenReader tokenReader) {

      this.redisManager = redisManager;
      this.tokenCreator = tokenCreator;
      this.jwtTokenReader = tokenReader;

   }
}
Sushrut Kanetkar
  • 363
  • 3
  • 13
0

The problem here is with the annotation you defined.

@ComponentScan("com.mongotest")

This will scan all the relevant packages under the project structure 'com.mongotest' and initializes the beans in all the subpackage classes.

Dheeraj
  • 332
  • 3
  • 4
-3

@Document(collection = "Students")
public class Student {
 
 @Id
 private String number;
 private String name;
 @Indexed(direction = IndexDirection.ASCENDING)
 private int classNo;

//Constructor and getters and setters.
}

================================

@Repository
public interface StudentRepository extends MongoRepository<Student, String>{

}

================================

@Component
@ComponentScan({"com.mongotest.repositories"})
public class Seeder implements CommandLineRunner{

 private StudentRepository studentRepo;
 
 public Seeder(StudentRepository studentRepo) {
  super();
  this.studentRepo = studentRepo;
 }

 @Override
 public void run(String... args) throws Exception {
  // TODO Auto-generated method stub

  Student s1 = new Student("1","Tom",1);
  Student s2 = new Student("2","Jerry",1);
  Student s3 = new Student("3","Kat",2);
  studentRepo.deleteAll();
  studentRepo.save(Arrays.asList(s1,s2,s3));
  
 }

}

================================

@SpringBootApplication
public class DemoApplication {
 public static void main(String[] args) {
  SpringApplication.run(DemoApplication.class, args);
 }
}