org.springframework.beans.factory.NoSuchBeanDefinitionException:类型'com.example.simple.RealApplication'的合格bean不可用

时间:2019-08-10 13:02:31

标签: java spring-boot component-scan

我无法理解为什么未注册/未找到我的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的软件包时,它就能找到它

1 个答案:

答案 0 :(得分:1)

您输入的代码有误吗?您显示的代码始终创建两个应用程序上下文,一个是AnnotationConfigApplicationContext,另一个是使用spring-boot方法创建的,其中涉及使用SpringApplication@SpringBootApplication

您总是从RealApplication创建的上下文中获取AnnotationConfigApplicationContext bean,而不是从Spring Boot创建的上下文中获取。要测试在后一种情况下是否确实创建了RealApplication bean,必须从SpringApplication.run()返回的上下文中获取bean:

ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
RealApplication app = context.getBean(RealApplication.class);
app.run();