解决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路径没有改

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

猜你喜欢

转载自blog.csdn.net/weixin_42727032/article/details/104574478