简体   繁体   English

通过字段“userRepository”表达的不满意依赖

[英]Unsatisfied dependency expressed through field 'userRepository'

Can't solve problems with test Spring Boot application:无法解决测试 Spring Boot 应用程序的问题:

spring.bat init --artifactId=dbproto3 --boot-version=2.1.7.RELEASE --dependencies=jdbc,data-rest,web,thymeleaf,devtools,lombok,configuration-processor,security,data-jpa,data-jdbc,postgresql,actuator --description=dbproto3 --groupId=com.test --java-version=11 --name=dbproto3 --package-name=com.test.dbproto3 --version=0.0.1-SNAPSHOT

It is a test application made by modifying Guide "Accessing data with MySQL" from official Spring site.它是通过修改 Spring 官方网站的“Accessing data with MySQL”指南制作的测试应用程序。 The first version (close to initial: empty springconfig, @Controller and @RequestMapping(path="/demo"), ...) worked great but my version generates an exception第一个版本(接近初始:空 springconfig、@Controller 和 @RequestMapping(path="/demo"), ...)效果很好,但我的版本产生了异常

Running it i get an exception:运行它我得到一个例外:

Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
  2 .   at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  3 .   at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  4 .   at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  5 .   at java.base/java.lang.reflect.Method.invoke(Method.java:566)
  6 .   at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
  7 . Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mainController': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.test.dbproto3.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  8 .   at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
  9 .   at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
 10 .   at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
 11 .   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411)
 12 .   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
 13 .   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
 14 .   at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
 15 .   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
 16 .   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
 17 .   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
 18 .   at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845)
 19 .   at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
 20 .   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
 21 .   at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:88)
 22 .   at com.test.dbproto3.Dbproto3Application.main(Dbproto3Application.java:18)
 23 .   ... 5 more
 24 . Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.test.dbproto3.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
 25 .   at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1658)
 26 .   at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1217)
 27 .   at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1171)
 28 .   at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
 29 .   ... 19 more

Dbproto3Application: Dbproto3应用程序:

@SpringBootApplication
  2 . public class Dbproto3Application {
  3 .
  4 .    public static void main(String[] args) {
  5 .         SpringApplication.run(Dbproto3Application.class, args);
  6 .
  7 .         ApplicationContext javaConfigContext = new AnnotationConfigApplicationContext(SpringConfig.class);
  8 .
  9 .         MainController mainControllerObj = (MainController) javaConfigContext.getBean("mainController");
 10 .
 11 .         mainControllerObj.addNewUser();
 12 .    }
 13 . }

SpringConfig:弹簧配置:

  1 . @Configuration
  2 . public class SpringConfig {
  3 .     @Bean("mainController")
  4 .     public MainController createMainController() {
  5 .         return new MainController();
  6 .     }
  7 . }

User:用户:

 1 . @Entity
  2 . @Table(name = "userstab")
  3 . public class User {
  4 .   @Id
  5 .   @GeneratedValue(strategy=GenerationType.AUTO)
  6 .   private Integer id;
  7 .
  8 .   private String name;
  9 .
 10 .   private String email;
 11 .
 12 . }

UserRepository:用户库:

  1 . public interface UserRepository extends CrudRepository<User, Integer> {
  2 .
  3 . }

MainController:主控制器:

  3 . public class MainController {
  4 .
  5 .     @Autowired
  6 .     private UserRepository userRepository;
  7 .
  8 .
  9 .     public void addNewUser() {
 10 .         User n = new User();
 11 .         n.setName("testuser");
 12 .         n.setEmail("testemail");
 13 .         System.out.println("addNewUser()");
 14 .         userRepository.save(n);
 15 .     }
 16 .
 17 . }

application properties:应用特性:

server.port = 8082

## default connection pool
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5


## PostgreSQL
spring.datasource.url=jdbc:postgresql://localhost:5432/example_db
spring.datasource.username=someuser
spring.datasource.password=123456

spring.jpa.hibernate.ddl-auto=update

I could not see any annotations for your repository interface in the code that you shared .Even though you are extending the CrudRepository interface to create your own interface.我在您共享的代码中看不到您的存储库接口的任何注释。即使您正在扩展CrudRepository接口以创建您自己的接口。 Spring boot needs to understand during startup that it needs to provide default implementation for your entities.Use annotation @Repository on your interface like : Spring boot 需要在启动期间了解它需要为您的实体提供默认实现。在您的界面上使用注释@Repository ,例如:

 @Repository
 public interface UserRepository extends CrudRepository<User, Integer> {
  
  }

Also, you are creating the controller bean in some weird way , so you might be attempting something unconventional which means just using @Autowired in your main application class might also be a bit of stretch for you.此外,您正在以某种奇怪的方式创建控制器 bean,因此您可能正在尝试一些非常规的东西,这意味着仅在您的主应用程序类中使用@Autowired对您来说也可能有点牵强。 But i would suggest doing that.但我会建议这样做。 You are using AnnotationConfigApplicationContext to create the bean for your controller but you are only providing the configuration class that you created for scanning.您正在使用AnnotationConfigApplicationContext为您的控制器创建 bean,但您只提供您为扫描创建的配置类。 Maybe the bean for UserRepository since it's getting created by springboot is not present in the application context that you created using AnnotationConfigApplicationContext .也许 UserRepository 的 bean 因为它是由 springboot 创建的,所以在您使用AnnotationConfigApplicationContext创建的应用程序上下文中不存在。 I think it's a standalone context.我认为这是一个独立的上下文。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过带有userRepository的构造函数参数1表示的不满意依赖性 - Unsatisfied dependency expressed through constructor parameter 1 with userRepository 创建名称为&#39;app.dev.demo.DemoApplicationTests&#39;的bean时出错:通过字段&#39;userrepository&#39;表达了不满意的依赖性 - Error creating bean with name 'app.dev.demo.DemoApplicationTests': Unsatisfied dependency expressed through field 'userrepository' 创建名称为&#39;userController&#39;的bean时出错:通过字段&#39;userRepository&#39;表示的不满意依赖性; - Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userRepository'; 创建名称为“ mainController”的bean时出错:通过字段“ userRepository”表示不满意的依赖性 - Error creating bean with name 'mainController': Unsatisfied dependency expressed through field 'userRepository' 通过“ productDao”字段表示的不满意依赖性 - Unsatisfied dependency expressed through field 'productDao' 通过字段&#39;appUserRepository&#39;表达的Spring不满意的依赖关系 - Spring Unsatisfied dependency expressed through field 'appUserRepository' 通过字段“ clientRepository”表示不满意的依赖关系; - Unsatisfied dependency expressed through field 'clientRepository'; 通过字段“ freemarkerConfig”表示的不满意依赖性 - Unsatisfied dependency expressed through field 'freemarkerConfig' 通过字段“jdbcTemplate”表达的不满意依赖 - Unsatisfied dependency expressed through field 'jdbcTemplate' 通过字段&#39;userTokenService&#39;表示的不满意依赖性 - Unsatisfied dependency expressed through field 'userTokenService'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM