前提・実現したいこと
Javaのspring bootを使用して、投稿式のアプリを作成したいと考えております。
今回はJPAを使ってMySQLからデータを引っ張ってきて表示するシステムを、とりあえず完成させたいです。
環境
ProductName: Mac OS X ProductVersion: 10.15.2 BuildVersion: 19C57 java version "13.0.2" 2020-01-14 Java(TM) SE Runtime Environment (build 13.0.2+8) Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing) ーエディター Eclipse Version: 2.3.0.v20191210-0610 Spring Tool Suite 4 Version: 4.5.1.RELEASE
発生している問題・エラーメッセージ
ビルドの際のエラーです。
*************************** APPLICATION FAILED TO START *************************** Description: Field repos in com.example.demo.HelloController required a bean of type 'com.example.demo.Repos' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'com.example.demo.Repos' in your configuration.
該当のソースコード
ファイル構造はこんな感じで同じパッケージに4つのJavaファイル全て入れています。
これがエラーの出ているページです
HelloController
1package com.example.demo; 2import org.springframework.beans.factory.annotation.Autowired; 3import org.springframework.stereotype.Controller; 4import org.springframework.web.bind.annotation.RequestMapping; 5import java.util.List; 6 7import org.springframework.ui.Model; 8 9@Controller 10public class HelloController { 11 @Autowired 12 public Repos repos; 13 //ここでビルドエラー 14 15 @RequestMapping("/") 16 public String index(Model model) { 17 model.addAttribute("message", "こんにちは"); 18 //ビルド成功後はこの辺でデータを引っ張り出したい 19 return "index"; 20 } 21}
エンティティ用のページです
Test
1package com.example.demo; 2import javax.persistence.Entity; 3import javax.persistence.GeneratedValue; 4import javax.persistence.GenerationType; 5import javax.persistence.Table; 6import javax.persistence.Id; 7 8@Entity 9@Table(name="java") 10public class Test { 11 @Id 12 @GeneratedValue(strategy = GenerationType.AUTO) 13 14 private Integer id; 15 private String title; 16 private String content; 17 18 public Integer getId() { 19 return id; 20 } 21 22 public void setId(Integer id) { 23 this.id = id; 24 } 25 26 public String getTitle() { 27 return title; 28 } 29 30 public void setTitle(String title) { 31 this.title = title; 32 } 33 34 public String getContent() { 35 return content; 36 } 37 38 public void setContent(String content) { 39 this.content = content; 40 } 41} 42 43
リポジトリ用のページです
Repos
1package com.example.demo; 2 3import org.springframework.data.jpa.repository.JpaRepository; 4 5public interface Repos extends JpaRepository<Test, Integer>{ 6 7}
試したこと
・@Autowiredは原則同じパッケージ内からしか呼ぶことができないのでそれを確認。
補足情報(FW/ツールのバージョンなど)
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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>hello</artifactId> <version>0.0.1-SNAPSHOT</version> <name>hello</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.propertiesです
spring.jpa.hibernate.ddl-auto=none spring.datasource.url=jdbc:mysql://localhost:3306/javaTest spring.datasource.username=root spring.datasource.password=
※補足情報
以前VSCodeを使った時は特にエラーは出ませんでしたのでツールの問題の可能性もあるかもしれません。
※以下ビルド手順です
SpringToolSuite4を使っております。
フォルダを右クリックして→実行→SpringBootApplicationです
※ご教授いただいた./mvnw clean packageに必要な物をダウンロードし、試しましたが
同じエラーが出てしまいます。

