簡體   English   中英

org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有類型為'com.example.simple.RealApplication'的合格bean

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM