![](/img/trans.png)
[英]Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
[英]org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.simple.RealApplication' available
我无法理解为什么未注册/未找到我的Bean RealApplication
。
文件DemoApplication.java:
package com.example.simple;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@SpringBootApplication
public class DemoApplication
{
public static void main(String[] args)
{
SpringApplication.run(DemoApplication.class, args);
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext();
ctx.refresh();
RealApplication app = ctx.getBean(RealApplication.class);
app.run();
ctx.close();
}
}
文件RealApplication.java:
package com.example.simple;
import org.springframework.stereotype.Component;
@Component
public class RealApplication
{
public void run()
{
System.out.println("Hello World!");
}
}
但是,奇怪的是,这段代码可以正常工作:
文件DemoApplication.java:
package com.example.simple;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class DemoApplication
{
public static void main(String[] args)
{
SpringApplication.run(DemoApplication.class, args);
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext("com.example.simple");
RealApplication app = ctx.getBean(RealApplication.class);
app.run();
ctx.close();
}
}
类RealApplication保持不变。
输出为:
Hello World!
我的Maven pom.xml看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>simple-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.inject/javax.inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
出于某些奇怪的原因,即使两个类是同一个程序包,当我仅使用@SpringBootApplication
批注时,组件扫描也不使用RealApplication Bean。
但是当我指定要在构造函数中扫描到AnnotationConfigApplicationContext
的程序包时可以找到它
您输入的密码有误吗? 您显示的代码始终创建两个应用程序上下文,一个是AnnotationConfigApplicationContext
,另一个是使用spring-boot方式创建的,其中涉及到使用SpringApplication
和@SpringBootApplication
。
而且,您总是从AnnotationConfigApplicationContext
创建的上下文中获取RealApplication
bean,而不是从Spring Boot创建的上下文中获取RealApplication
bean。 要测试在后一种情况下是否真的创建了RealApplication
bean,必须从SpringApplication.run()
返回的上下文中获取bean:
ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
RealApplication app = context.getBean(RealApplication.class);
app.run();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.