Consider defining a bean of type in your configuration

Chase :

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>
Saravana :

Please add below annotations in DemoApplication

@SpringBootApplication
@ComponentScan("com.mongotest") //to scan packages mentioned
@EnableMongoRepositories("com.mongotest") //to activate MongoDB repositories
public class DemoApplication { ... }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to create spring @bean of type RestControllerAdvice in spring configuration class

Consider revisiting the entries above or defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration

Consider defining a bean of type 'package' in your configuration [Spring-Boot]

Consider defining a bean of type 'service' in your configuration [Spring boot]

Consider defining a bean of type 'redis.clients.jedis.JedisPool' in your configuration. error while integrating Redis Jedis

Consider defining a bean of type 'org.springframework.data.mongodb.core.MongoTemplate' in your configuration

Spring Boot - Injecting Repository into Controller throws Consider defining a bean of type 'Type' in your configuration

Consider defining a bean of type 'UserConverter' in your configuration

only after refactoring packages names, Consider defining a bean of type * in your configuration

Consider defining a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' in your configuration

Spring Boot defining bean in configuration problem

Consider defining a bean of type * in your configuration

spring boot fails to start-- define a bean of type 'TopicRepository' in configuration

Consider defining a bean of type 'com.gisapp.gisapp.dao.IUserDAO' in your configuration

Field in required a bean of type that could not be found consider defining a bean of type in your configuration

Redis Issue Consider defining a bean of type 'org.springframework.data.redis.core.HashOperations' in your configuration

Spring Boot @Mapper Bean creation issue : Application Failed to start. Error : Consider defining a bean of Type

Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration

Consider defining a bean of type 'org.flywaydb.core.Flyway'

Consider defining a bean of type 'com.ensat.services.ProductService' in your configuration

SpringBootTest - Consider defining a bean of type 'java.lang.String' in your configuration

How do i resolve Consider defining a bean of type 'java.lang.String' in your configuration?

Spring Boot: Consider defining a bean named 'entityManagerFactory' in your configuration

Consider defining a bean of type 'javax.servlet.ServletContext' in your configuration

APPLICATION FAILED TO START - Consider defining a bean of type in your configuration [SpringBoot]

Spring configuration set up : No qualifying bean of type [] found for dependency

Consider defining a bean of type 'io.ipl.amaresh.data.JobCompletionNotificationListener' in your configuration. The bean could not be found in that

How to fix "Consider defining a bean of type 'org.jooq.DSLContext' in your configuration." after update to jOOQ 3.15.0

How to fix this issue "Consider defining a bean of type 'repository.InterfaceName' in your configuration."