前提・実現したいこと
springでログインページを作成しています。
mysqlから予め入っているデータを取り出し、データが存在すればログイン成功ページに遷移するような簡単なページです。
発生している問題・エラーメッセージ
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'userRepos'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.sample.model.dao.UserRepository': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.sample.model.dao.UserRepository]: Specified class is an interface at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.9.jar:5.3.9] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.9.jar:5.3.9] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.4.jar:2.5.4] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) [spring-boot-2.5.4.jar:2.5.4] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) [spring-boot-2.5.4.jar:2.5.4] at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) [spring-boot-2.5.4.jar:2.5.4] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) [spring-boot-2.5.4.jar:2.5.4] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) [spring-boot-2.5.4.jar:2.5.4] at com.sample.SystemApplication.main(SystemApplication.java:14) [classes/:na] Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.sample.model.dao.UserRepository': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.sample.model.dao.UserRepository]: Specified class is an interface at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1316) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.9.jar:5.3.9] ... 20 common frames omitted Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.sample.model.dao.UserRepository]: Specified class is an interface at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:70) ~[spring-beans-5.3.9.jar:5.3.9] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1308) ~[spring-beans-5.3.9.jar:5.3.9] ... 31 common frames omitted
該当のソースコード
LoginController.java
1package com.sample.controller; 2 3import java.util.List; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.context.annotation.Import; 7import org.springframework.stereotype.Controller; 8import org.springframework.ui.Model; 9import org.springframework.web.bind.annotation.RequestMapping; 10import org.springframework.web.bind.annotation.RequestMethod; 11import org.springframework.web.bind.annotation.RequestParam; 12 13import com.sample.model.dao.UserRepository; 14import com.sample.model.entity.User; 15 16@Import(UserRepository.class) 17@Controller 18public class LoginController { 19 20 @Autowired 21 public UserRepository userRepos; 22 23 @RequestMapping(value="/" ,method = RequestMethod.GET) 24 public String index() { 25 return "index"; 26 } 27 28 @RequestMapping(value="/login" , method = RequestMethod.POST) 29 public String login( 30 @RequestParam(name = "i_UserCode") String i_UserCode, 31 @RequestParam(name = "i_Password") String i_Password, 32 @RequestParam(name = "i_TenantId") int i_TenantId, 33 Model model) { 34 35 String message = "welcome!" ; 36 37 List<User> users = userRepos.findByI_UserCodeAndI_PasswordAndI_TenantId(i_UserCode,i_Password,i_TenantId); 38 39 if(users.size() > 0) { 40 User user = users.get(0); 41 message += user; 42 } else { 43 message += "guest"; 44 } 45 46 model.addAttribute("message",message); 47 48 return "login"; 49 50 } 51 52} 53
UserRepository.java
1package com.sample.model.dao; 2 3import java.util.List; 4 5import org.springframework.data.jpa.repository.JpaRepository; 6import org.springframework.stereotype.Repository; 7 8import com.sample.model.entity.User; 9 10@Repository 11public interface UserRepository extends JpaRepository<User, String> { 12 13 List<User> findByI_UserCodeAndI_PasswordAndI_TenantId(String i_UserCode, String i_Password, int i_TenantId); 14} 15
User.java
1package com.sample.model.entity; 2 3import javax.persistence.Column; 4import javax.persistence.Entity; 5import javax.persistence.GeneratedValue; 6import javax.persistence.GenerationType; 7import javax.persistence.Id; 8import javax.persistence.Table; 9 10@Entity 11@Table(name="c_Info") 12public class User { 13 14 @Id 15 @GeneratedValue(strategy=GenerationType.IDENTITY) 16 @Column(name="i_UserCode",nullable=false) 17 private String i_UserCode; 18 19 @Column(name="i_Password",nullable=false) 20 private String i_Password; 21 22 @Column(name="i_TenantId",nullable=false) 23 private int i_TenantId; 24 25 public String getI_UserCode() { 26 return i_UserCode; 27 } 28 29 public void setI_UserCode(String i_UserCode) { 30 this.i_UserCode = i_UserCode; 31 } 32 33 public String getI_Password() { 34 return i_Password; 35 } 36 37 public void setI_Password(String i_Password) { 38 this.i_Password = i_Password; 39 } 40 41 public int getI_TenantId() { 42 return i_TenantId; 43 } 44 45 public void setI_TenantId(int i_TenantId) { 46 this.i_TenantId = i_TenantId; 47 } 48 49 50 51 52} 53
試したこと
UserRepositoryとRoginControllerの間で何か問題があるのはわかるのですが、原因がわかりません。
インスタンスが形成されてないのでしょうか?
補足情報(FW/ツールのバージョンなど)
java8
Mac os
MYSQL 5.7.35
Spring boot
Spring Tool Suite 4.8.1
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。