如何解决通过字段“ userRepository”表示的不满意的依赖性
无法解决测试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
这是一个通过修改Spring官方网站上的指南“使用MysqL访问数据” 制成的测试应用程序。第一个版本(接近初始版本:空的springconfig,@ Controller和@RequestMapping(path =“ / demo”),...)效果很好,但我的版本会生成异常
运行它时出现异常:
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:
@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 . }
用户:
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 . }
用户存储库:
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 . }
应用程序属性:
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
解决方法
即使您扩展CrudRepository
接口以创建自己的接口,我在共享的代码中也看不到存储库接口的任何注释。 Spring Boot需要在启动过程中了解它需要为您的实体提供默认实现。请在界面上使用注释@Repository
,例如:
@Repository
public interface UserRepository extends CrudRepository<User,Integer> {
}
此外,您正在以某种怪异的方式创建控制器bean,因此您可能会尝试一些非常规的操作,这意味着仅在主应用程序类中使用@Autowired
可能对您来说也有些麻烦。但是我建议这样做。您正在使用AnnotationConfigApplicationContext
为控制器创建bean,但是仅提供您创建用于扫描的配置类。可能是UserRepository的bean,因为它是由springboot创建的,它不存在于使用AnnotationConfigApplicationContext
创建的应用程序上下文中。我认为这是一个独立的上下文。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。