当我尝试运行Spring Boot应用程序时,出现了这个错误
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.mongodb.core.MongoOperations' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1714) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1270) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1224) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:884) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:788) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:538) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:893) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:62) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at com.greglturnquist.learningspringboot2.learningspringboot2.LearningSpringBoot2Application.main(LearningSpringBoot2Application.java:12) [main/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.3.1.RELEASE.jar:2.3.1.RELEASE]
就我所知,我的代码如下,我有一个MongoOperations
bean:
@Component
public class InitDatabase {
@Bean
CommandLineRunner init(MongoOperations operations) {
return args -> {
// tag::log[]
operations.dropCollection(Image.class);
operations.insert(new Image("1",
"learning-spring-boot-cover.jpg"));
operations.insert(new Image("2",
"learning-spring-boot-2nd-edition-cover.jpg"));
operations.insert(new Image("3",
"bazinga.png"));
operations.findAll(Image.class).forEach(image -> {
System.out.println(image.toString());
});
// end::log[]
};
}
}
我做错了什么?
我认为您可能错误地使用了@Component
注释来声明配置类,请使用@Configuration
来代替:
@Configuration
public class InitDatabase {
@Bean
CommandLineRunner init(MongoOperations operations) {
return args -> {
...
};
}
}
从文件中:
配置:表示一个类声明了一个或多个@Bean方法,并且可以由Spring容器处理,以便在运行时为这些Bean生成Bean定义和服务请求。
问题内容: 我有一个延伸的四季豆。我希望将此bean注入控制器中,但是我不希望它实现任何接口。我只想从其余代码中引用具体的类(也许不使用AopProxy?)有人可以这样做吗? 我知道使用该接口是一个好主意,这就是IoC的重点,但是请不要建议我使用该接口。 问题答案: 如果要代理的类(在您的情况下是通过事务代理实现)实现了任何接口(在您的情况下是通过实现),则Spring默认使用基于JDK动态代理的
当我尝试在junit测试类中加载配置文件时,在注入数据源时出现以下错误: 组织。springframework。豆。工厂NoSuchBeanDefinitionException:在组织中未定义名为“transactionManager”的bean。springframework。豆。工厂支持DefaultListableBeanFactory。位于org的getBeanDefinition(De
嗨,我是Spring的新手:我有一个与我的项目配置相关的问题; 这是我的控制器: 它是非常基本的,当我在服务器上运行它时,我得到了这个错误:
Oauth2,如下所示: 在访问回调url时,我得到以下异常: org.springframework.beans.factory.BeanCreationException:创建名为“ScopedTarget.oauth2ClientContext”的bean时出错:当前线程的作用域“session”不活动;如果您打算从单个对象引用这个bean,请考虑为它定义一个限定作用域的代理 完全异常跟踪:
问题内容: 我已经用实体管理器配置了两个持久性单元,如下所示: 然后,我将事务管理器配置为 我最初只有一个配置,它被称为“ transactionManager”。Addint一个附加的持久性单元似乎会生成错误。我不明白的一件事,如果我配置了两个持久性单元(每个持久性单元用于一个单独的数据库),是否还需要为每个数据源配置一个单独的实体管理器和一个事务管理器? 我得到的错误如下所示:(我搜索了所有文
我正在尝试使用基于Java的配置而不是来自这篇博文的XML配置将配置引入Spring Boot:http://blog.novoj.net/2012/03/27/combining-custom-annotations-for-securing-methods-with-spring-security/ 我得到以下例外: org . spring framework . beans . facto