无法解决测试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
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
@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 . }
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
,如下所示:
@Repository
public interface UserRepository extends CrudRepository<User, Integer> {
}
此外,您正在以某种奇怪的方式创建controller bean,因此您可能正在尝试一些非常规的方法,这意味着仅仅在主应用程序类中使用@autowired
也可能会使您感到有点吃力。但我建议你这么做。您正在使用AnnotationConfigApplicationContext
为您的控制器创建bean,但您只提供了为扫描而创建的配置类。也许UserRepository的bean(因为它是由springboot创建的)不存在于您使用AnnotationConfigApplicationContext
创建的应用程序上下文中。我认为这是一个独立的背景。
大家好,每个人都是spring的新手,正在尝试为CRUD创建一个简单的应用程序,但是我得到了下面的错误。我正在使用STS 创建名为“Client ServiceImpli”的bean时出错:通过字段“Client Repository”表示的不满足的依赖关系;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“clie
以下是例外情况: 我的代码: 我评论了setCacheable(true),因为它给出了一个错误。有人能帮忙吗?
正在尝试测试我的。。。编译器请求注入,但在为创建bean后,它返回通过字段 代码: 错误:
错误:-->org.springframework.beans.factory.unsatisfieddependencyexception:创建名为“home controller”的bean时出错:通过字段“user tokenservice”表示的不满足的依赖项;嵌套异常为org.springframework.beans.factory.unsatisfieddependencyexcep
在一个使用jwt的Spring Boot应用程序中,在我的spring-cloud-gateway中,我有以下代码。 [main]onfigReactiveWebServerApplicationContext:上下文初始化过程中遇到异常-取消刷新尝试:org.springframework.beans.factory.unsatisfiedDependencyException:创建文件[/ho