No qualifying bean of type 'com.yubai.el.ELConfig' available

本文介绍了一个关于 Spring 框架中 Bean 注入失败的问题及其解决方案。问题出现在尝试通过 AnnotationConfigApplicationContext 加载一个名为 ELConfig 的配置类时,由于配置方式错误导致系统无法识别该 Bean。文章详细解释了如何正确地指定配置类,并展示了修改后的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Aug 04, 2017 3:36:39 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@49097b5d: startup date [Fri Aug 04 15:36:39 CST 2017]; root of context hierarchy

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.yubai.el.ELConfig' available

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090)
    at com.yubai.el.Main.main(Main.java:9)

源代码:

package com.yubai.el;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("ELConfig.class");

        ELConfig resourceService = context.getBean(ELConfig.class);
        resourceService.outputResource();
        context.close();
    }
}

出错原因:

从错误信息可知:没有此bean注入容器中,所以可能没有用@Service或者@Bean注入此类。还有可能是别的错误导致,如上错误。

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(“ELConfig.class”);
AnnotationConfigApplicationContext作为spring容器,接收输入一个配置类作为参数。接收的类不要加双引号。

修改代码如下:

package com.yubai.el;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ELConfig.class);

        ELConfig resourceService = context.getBean(ELConfig.class);
        resourceService.outputResource();
        context.close();
    }
}

此时运行正常

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值