【问题标题】:Spring boot consider defining a bean of type, component scan not workingSpring boot考虑定义一个类型的bean,组件扫描不起作用
【发布时间】:2019-03-21 06:28:23
【问题描述】:

我正在尝试遵循以下教程:

https://dzone.com/articles/spring-boot-jpa-hibernate-oracle

我的项目结构如下:

我的pom如下:

<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.nuril.work</groupId>
  <artifactId>SpringBootHiberate</artifactId>
   <version>0.0.1-SNAPSHOT</version>

   <properties>
       <maven.compiler.source>1.8</maven.compiler.source>
       <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>
  <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
  </dependencies>
  <build>
      <plugins>
          <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
      </plugins>
  </build>

</project>

当我运行如图所示的Application类时:

@SpringBootApplication
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class Application implements CommandLineRunner{
    @Autowired
    SoccerService soccerService;
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    @Override
    public void run(String... arg0) throws Exception {
        soccerService.addBarcelonaPlayer("Xavi Hernandez", "Midfielder", 6);
        List<String> players = soccerService.getAllTeamPlayers(1);
        for(String player : players)
        {
            System.out.println("Introducing Barca player => " + player);
        }
    }
}

我收到以下错误:

Description:

Field playerRepository in com.nuril.work.service.SoccerServiceImpl required a bean of type 'com.nuril.work.repository.PlayerRepository' that could not be found.


Action:

Consider defining a bean of type 'com.nuril.work.repository.PlayerRepository' in your configuration.

我查看了其他答案,他们建议添加 @ComponentScan 注释。

我添加了以下内容

@SpringBootApplication
@ComponentScan("com.nuril.work.repository") 
@ComponentScan("com.nuril.work.service") 

但是我仍然遇到同样的错误,这可能是什么原因?

【问题讨论】:

  • 您可能在 PlayerRepository 上缺少 @Component 注释
  • 为什么排除了DataSourceAutoConfigurationHibernateJpaAutoConfiguration
  • 因为它消除了错误:描述:无法确定数据库类型的嵌入式数据库驱动程序类无操作:如果您想要一个嵌入式数据库,请在类路径中放置一个受支持的数据库。如果您有要从特定配置文件加载的数据库设置,您可能需要激活它(当前没有配置文件处于活动状态)。
  • 那是因为你还没有定义你的数据源。
  • 是的,我的文件被称为 app.properties 而不是 application.properties,更改文件名后我不再收到任何错误。

标签: java spring spring-boot


【解决方案1】:

尝试将@EnableJpaRepositories(basePackages="com.nuril.work.repository") 添加到Application 类中。

参见文档:https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/config/EnableJpaRepositories.html

另外,请检查您的存储库是否有 @Repository 注释。

【讨论】:

    猜你喜欢
    • 2019-12-05
    • 2022-06-17
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 2015-12-12
    • 2017-03-16
    • 2018-08-03
    • 2017-05-30
    相关资源
    最近更新 更多