Spring 框架主方法运行后 异常 No bean named 'XXX' is defined

本文记录了使用Spring框架加载applicationContext.xml配置文件时遇到的NoSuchBeanDefinitionException异常,并展示了如何通过正确指定Bean名称解决问题的过程。

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

十月 16, 2017 3:19:37 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@426a086a: startup date [Mon Oct 16 15:19:37 CST 2017]; root of context hierarchy
十月 16, 2017 3:19:37 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
十月 16, 2017 3:19:38 下午 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
十月 16, 2017 3:19:38 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4f5b0046: defining beans [empDao01,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,template,dbcp,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'empDao02' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:570)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1114)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:279)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121)

    at org.show51.testMain.main(testMain.java:29)




原代码:

public class testMain {
    public static void main(String[] args) {
        String conf="applicationContext.xml";
        ApplicationContext ac=
                new ClassPathXmlApplicationContext(conf);
        EmpDao01 empDao01=ac.getBean("empDao02", EmpDao01.class);
        Emp emp01=new Emp();
        emp01.setAdminId("9");
        emp01.setAdminname("tian");
        emp01.setAdminpwd("111");
        empDao01.save(emp01);
    }
}



原EmpDao01代码

@Repository
    public class EmpDao01{
    @Resource//注入
        private JdbcTemplate template;
        public void save(Emp emp){
            String sql="insert into t_test"
                    +"(adminId,adminname,adminpwd) "
                    + "values (?,?,?)";
            Object[] param={
                    emp.getAdminId(),
                    emp.getAdminname(),
                    emp.getAdminpwd()
            };
            template.update(sql, param);
        }
    }
 


报出问题就在于: EmpDao01 empDao01=ac.getBean("empDao02", EmpDao01.class);


的getBean(arg0,arg1),这个arg0的参数要是你EmpDao01  类的首字母缩写,这里实际是从这个类中获取bean对象,你要写的话就是将这个类名首字母缩写填入arg0就行了



再次运行:

十月 16, 2017 3:25:41 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6c069ec: startup date [Mon Oct 16 15:25:41 CST 2017]; root of context hierarchy
十月 16, 2017 3:25:41 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
十月 16, 2017 3:25:41 下午 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
十月 16, 2017 3:25:41 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7ce5bef1: defining beans [empDao01,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,template,dbcp,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
查询一下mysql数据库:
有这么一条数据 id:9;用户名:tian;密码:111的数据

### SSH框架中解决 `NoSuchBeanDefinitionException` 的方法 当遇到 `NoSuchBeanDefinitionException` 异常提示 `'byPlatPushService'` 未定义 bean 时,通常意味着 Spring 容器未能找到名为 `byPlatPushService` 的 Bean 实例。这可能是由于多种原因造成的。 #### 可能的原因分析 1. **缺少必要的注解** 如果使用基于注解的方式配置,则需要确保目标类上已正确应用了相应的注解(如 `@Component`, `@Service`, 或者自定义的命名空间)。对于服务层组件推荐使用 `@Service` 注解[^2]。 2. **组件扫描路径不匹配** 需要确认应用程序上下文中已经启用了组件自动扫描功能,并且指定了正确的基础包名来查找带有特定注解的候选组件。可以通过设置 `<context:component-scan base-package="com.example"/>` 来实现这一点[^3]。 3. **拼写错误或名称不符** 检查是否存在大小写的差异或其他形式上的误打字情况;另外还要注意依赖关系中的名字是否一致。 4. **配置文件缺失或错误** 若采用 XML 方式管理 Bean,则应核实是否有遗漏引入外部资源文件的情况发生,以及这些文件内部的内容准确性如何。 #### 解决方案建议 为了有效处理上述提到的各种可能性,在此提供一种较为通用的做法: - 确认 `ByPlatPushServiceImpl.java` 文件顶部存在如下声明: ```java package com.ssh.service.impl; import org.springframework.stereotype.Service; @Service("byPlatPushService") // 显式指定bean name public class ByPlatPushServiceImpl implements ByPlatPushService { ... } ``` - 修改启动类以支持组件扫描并加载所需的服务接口实现类: ```java package com.ssh; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @ComponentScan(basePackages={"com.ssh"})// 添加这一行用于开启组件扫描 public class SshApplication { public static void main(String[] args) { SpringApplication.run(SshApplication.class, args); } } ``` 通过以上调整可以使得 Spring Boot 应用能够成功注册所需的 `byPlatPushService` Bean 并正常工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值