【问题标题】:org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.simple.RealApplication' availableorg.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“com.example.simple.RealApplication”类型的合格bean
【发布时间】:2019-08-10 13:02:31
【问题描述】:

我无法理解为什么我的 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时它能够找到它

【问题讨论】:

    标签: java spring-boot component-scan


    【解决方案1】:

    您是否显示错误的代码?您显示的代码总是创建两个应用程序上下文,一个是 AnnotationConfigApplicationContext,另一个是使用 spring-boot 方式创建的,其中涉及使用 SpringApplication 和 @SpringBootApplication。

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

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

    【讨论】:

    • 谢谢,肯。问题是 new AnnotationConfigApplicationContext() 本身没有注册任何 bean。我们需要传递一个Configuration 或Component 的类。组件扫描可以通过使用 ComponentScan 注释该类或使用 AnnotationConfigApplicationContext.scan() 或 AnnotationConfigApplicationContext("package") 来触发。但是您还在我的代码中指出了另一个问题,即创建冗余ApplicationContext。我是初学者,所以我不小心射中了自己的腿。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 2020-06-10
    • 2019-02-20
    相关资源
    最近更新 更多