
前提・実現したいこと
画面に入力した従業員IDを使ってDBに検索を行い、その検索結果を画面に出力するというサンプルプログラムです。
実行時に下記のエラーが出てしまいます。
UnsatisfiedDependencyExceptionというエラー文から推測するに、DIの注入?(まだ初心者なのでほとんどわかりません)が
できていないということなのでしょうか?
ご回答よろしくお願いします。
コンソールログ
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-07-10 22:49:57.872 ERROR 16628 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloController': Unsatisfied dependency expressed through field 'helloService'; nested exception is org.springframework.beans.factory. UnsatisfiedDependencyException: Error creating bean with name 'helloService': Unsatisfied dependency expressed through field 'helloRepository'; nested exception is org.springframework.beans.factory. UnsatisfiedDependencyException: Error creating bean with name 'helloRepository': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory. UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration$JdbcTemplateConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException: Property spring.datasource.schema with value 'class path resource [schema.sql]' is invalid: The specified resource does not exist.
ソースコード
pom
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.6.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.example</groupId> 12 <artifactId>SpringSample</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>SpringSample</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> 20 </properties> 21 22 <dependencies> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-jdbc</artifactId> 26 </dependency> 27 <dependency> 28 <groupId>org.springframework.boot</groupId> 29 <artifactId>spring-boot-starter-thymeleaf</artifactId> 30 </dependency> 31 <dependency> 32 <groupId>org.springframework.boot</groupId> 33 <artifactId>spring-boot-starter-web</artifactId> 34 </dependency> 35 36 <dependency> 37 <groupId>org.springframework.boot</groupId> 38 <artifactId>spring-boot-devtools</artifactId> 39 <scope>runtime</scope> 40 <optional>true</optional> 41 </dependency> 42 <dependency> 43 <groupId>com.h2database</groupId> 44 <artifactId>h2</artifactId> 45 <scope>runtime</scope> 46 </dependency> 47 <dependency> 48 <groupId>org.projectlombok</groupId> 49 <artifactId>lombok</artifactId> 50 <optional>true</optional> 51 <scope>provided</scope> 52 </dependency> 53 <dependency> 54 <groupId>org.springframework.boot</groupId> 55 <artifactId>spring-boot-starter-test</artifactId> 56 <scope>test</scope> 57 </dependency> 58 </dependencies> 59 60 <build> 61 <plugins> 62 <plugin> 63 <groupId>org.springframework.boot</groupId> 64 <artifactId>spring-boot-maven-plugin</artifactId> 65 </plugin> 66 </plugins> 67 </build> 68 69</project> 70
SpringSampleApplication
1package com.example.demo; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6@SpringBootApplication 7public class SpringSampleApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(SpringSampleApplication.class, args); 11 } 12 13}
HelloService
1package com.example.demo; 2 3import java.util.Map; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Service; 7 8@Service 9public class HelloService { 10 11 @Autowired 12 private HelloRepository helloRepository; 13 14 public Employee findOne(int id) { 15 16 Map<String, Object>map = helloRepository.findOne(id); 17 18 int employeeId = (Integer)map.get("employee_id"); 19 String employeeName = (String)map.get("employee_name"); 20 int age = (Integer)map.get("age"); 21 22 Employee employee = new Employee(); 23 employee.setEmployeeId(employeeId); 24 employee.setEmployeeName(employeeName); 25 employee.setAge(age); 26 27 return employee; 28 29 } 30} 31
HelloRepository
1package com.example.demo; 2 3import java.util.Map; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.jdbc.core.JdbcTemplate; 7import org.springframework.stereotype.Repository; 8 9@Repository 10public class HelloRepository { 11 12 @Autowired 13 private JdbcTemplate jdbcTemplate; 14 15 public Map<String, Object>findOne(int id){ 16 17 String query = "SELECT " 18 + "Employee_id " 19 + "employee_name " 20 + "age " 21 + "FROM employee " 22 + "WHERE employee_id=?"; 23 24 Map<String, Object> employee = jdbcTemplate.queryForMap(query, id); 25 26 return employee; 27 } 28} 29
HelloController
1package com.example.demo; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.stereotype.Controller; 5import org.springframework.ui.Model; 6import org.springframework.web.bind.annotation.GetMapping; 7import org.springframework.web.bind.annotation.PostMapping; 8import org.springframework.web.bind.annotation.RequestParam; 9 10@Controller 11public class HelloController { 12 13 @Autowired 14 private HelloService helloService; 15 16 @GetMapping("/hello") 17 private String getHello(){ 18 return "hello"; 19 } 20 21 @PostMapping("/hello") 22 public String postRequest(@RequestParam("text1")String str, Model model ) { 23 24 model.addAttribute("sample", str); 25 26 return "helloResponse"; 27 } 28 29 @PostMapping("/hello/db") 30 public String postDbRequest(@RequestParam("text2")String str, Model model) { 31 32 int id = Integer.parseInt(str); 33 34 Employee employee = helloService.findOne(id); 35 36 model.addAttribute("id", employee.getEmployeeId()); 37 model.addAttribute("name", employee.getEmployeeName()); 38 model.addAttribute("age", employee.getAge()); 39 40 return "helloResponseDB"; 41 } 42}
Employee
1package com.example.demo; 2 3import lombok.Data; 4 5@Data 6public class Employee { 7 8 private int employeeId; 9 private String employeeName; 10 private int age; 11 12 13} 14
データベース
schma
1CREATE TABLE IF NOT EXISTS employee( 2employee_id INT PRIMARY KEY, 3employee_name VARCHAR(50), 4age INT 5);
data
1INSERT INTO employee(employee_id, employee_name, age) VALUES (1, '山田太郎', 30);
補足情報(FW/ツールのバージョンなど)
Eclipse Photon(4.8.0)
STS 3.9.7
Maven 3.1.1
application.properties
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE spring.datasource.driver-class-name=org.h2.Driver spring.datasouce.username=sa spring.datasouce.password= spring.datasource.sql-script-encoding=UTF-8 spring.h2.console.enabled=true spring.datasource.initialize=true spring.datasource.schema=classpath:schema.sql spring.datasource.data=classpath:data.sql



回答1件
あなたの回答
tips
プレビュー