Spring Boot考虑定义类型的bean,组件扫描不起作用

时间:2019-03-20 22:21:00

标签: java spring spring-boot

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

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

我的项目结构如下:

enter image description here

我的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") 

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

1 个答案:

答案 0 :(得分:0)

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

查看文档:{​​{3}}

还要检查您的存储库是否带有@Repository批注。