解决Spring(Component注解)错误:NoSuchBeanDefinitionException: No bean named 'XXXXXXX' available

前提需要

需要这个jar包
在这里插入图片描述

我们也需要告知spring
在这里插入图片描述
将这些代码放在bean.xml中
xxxx为你的根目录

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        <!--这时候会扫描这个包及其子包下的所有类上或接口上的注解-->
        <context:component-scan base-package="XXXX">
        </context:component-scan>
</beans>

Component

value:用于指定bean的id。当我们不写时,它的默认值时当前类名,且首字母改小写
在这里插入图片描述在这里插入图片描述
在实现的时候改成
在这里插入图片描述

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //用注解搞把类名复制过来改成小写
        IAccountService as = (IAccountService)ac.getBean("accountServiceImpl");
        System.out.println(as);
    }

或者是你value自己命名 就相当于配置里面的id

<bean id="accountService" class="bruce.service.impl.AccountServiceImpl"

我的问题主要就是没注意把前面大写字母改成小写
另一个问题就是我是复制过来的文件,package路径没有改

注意了,就好了希望能帮助到你

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值