【问题标题】:Consider defining a bean of type in your configuration考虑在你的配置中定义一个 bean 类型
【发布时间】:2018-06-22 17:49:50
【问题描述】:

我正在关注本教程 (https://www.youtube.com/watch?v=Hu-cyytqfp8) 并尝试在 Spring Boot 中连接到远程服务器上的 MongoDB。 我在运行应用程序时收到以下消息。

说明:com.mongotest.demo.Seeder中构造函数的参数0 需要一个 'com.mongotest.repositories.StudentRepository' 类型的 bean 找不到。

操作:考虑定义一个 bean 类型 'com.mongotest.repositories.StudentRepository' 在你的配置中。

项目结构。

这是我的课程

    @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>

【问题讨论】:

  • stackoverflow.com/a/45012168/3344829,你能试试spring boot版本1.5.1或更低
  • 我试过 1.5.1.RELEASE 和 1.5.0.RELEASE... 还是不行
  • 我们需要使用@EnableMongoRepositories激活mongo仓库,请看答案
  • 尝试使用 @Service 注释 StudentRepository 类。我在另一个 answer 上看到了这个,这对我有用。
  • @Lycanthropeus a Repository is not a Service, 也许应该注释为@Repository?

标签: java spring mongodb spring-boot


【解决方案1】:

请在DemoApplication中添加以下注释

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

【讨论】:

  • 你能补充一些解释吗?
【解决方案2】:

如果您希望避免编写注释,您可以简单地将您的包 com.mongotest.entities 更改为 com.mongotest.demo.entitiescom.mongotest.repositories 更改为 com.mongotest.demo.repositories

Spring Boot 架构将负责休息。实际上,其他文件和包应该处于同一级别或低于您的DemoApplication.java

【讨论】:

    【解决方案3】:

    在我的情况下,我使用 mysql db 遇到了同样的错误

    使用 @EnableJpaRepositories

    解决了
    @SpringBootApplication
    @ComponentScan("com.example.repositories")//to scan repository files
    @EntityScan("com.example.entities")
    @EnableJpaRepositories("com.example.repositories")
    public class EmployeeApplication implements CommandLineRunner{ ..}
    

    【讨论】:

      【解决方案4】:

      我有 3 个辅助类 RedisManager、JWTTokenCreator 和 JWTTokenReader,我需要将它们作为 SessionService spring 服务的依赖项传入构造函数。

      @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);
        }
      }
      

      服务类如下

      @Service
      @Component
      public class SessionService {
        @Autowired
        public SessionService(RedisManager redisManager, JWTTokenCreator 
            tokenCreator, JWTTokenReader tokenReader) {
      
            this.redisManager = redisManager;
            this.tokenCreator = tokenCreator;
            this.jwtTokenReader = tokenReader;
      
         }
      }
      

      【讨论】:

        【解决方案5】:

        @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);
        	}
        }

        【讨论】:

        • 这根本行不通。
        【解决方案6】:

        这里的问题在于您定义的注释。

        @ComponentScan("com.mongotest")

        这将扫描项目结构'com.mongotest'下的所有相关包并初始化所有子包类中的bean。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-08-01
          • 2019-02-14
          • 2018-12-21
          • 2019-05-10
          • 2019-09-04
          • 2020-05-23
          • 2019-02-05
          • 1970-01-01
          相关资源
          最近更新 更多