问题描述
无法解决测试 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"), ...)效果很好,但我的版本产生了异常
It is a test application made by modifying Guide "Accessing data with MySQL" from official Spring site. The first version (close to initial: empty springconfig, @Controller and @RequestMapping(path="/demo"), ...) worked great but my version generates 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:
@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 . }
主控制器:
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
,例如:
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. Spring boot needs to understand during startup that it needs to provide default implementation for your entities.Use annotation @Repository
on your interface like :
@Repository
public interface UserRepository extends CrudRepository<User, Integer> {
}
此外,您正在以某种奇怪的方式创建控制器 bean,所以您可能正在尝试一些非常规的东西,这意味着在您的主应用程序类中仅使用 @Autowired
对您来说也可能有点牵强.但我会建议这样做.您正在使用 AnnotationConfigApplicationContext
为您的控制器创建 bean,但您只提供您为扫描创建的配置类.也许 UserRepository 的 bean 是由 springboot 创建的,因此在您使用 AnnotationConfigApplicationContext
创建的应用程序上下文中不存在.我认为这是一个独立的上下文.
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. 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. Maybe the bean for UserRepository since it's getting created by springboot is not present in the application context that you created using AnnotationConfigApplicationContext
. I think it's a standalone context.
这篇关于通过字段“userRepository"表达的不满意依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!