nosuchbeandefinitionexception在xml中定义bean时出现异常

gblwokeq  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(212)

我试图引用一个使用@resource注解在xml文件中定义的bean,但是我一直得到一个 org.springframework.beans.factory.NoSuchBeanDefinitionException 例外。
以下是文件:
employee.java文件:

public class Employee {
   private String name;
   private int age;

   //getter and setter methods
}

employeemanager.java:

@Service("employeeManager")
public class EmployeeManager {

   @Resource(name="employeeList")
   private List<Employee> employeeList;

   //other methods here
}

applicationcontext.xml:

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
               http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

    <bean id="employeeList" class="java.util.ArrayList">
        <constructor-arg>
            <list>
               <ref bean="bob" />
               <ref bean="sally" />
            </list>
        </constructor-arg>
    </bean>

    <bean id="bob" class="Employee">
        <property name="name" value="Bob"/>
        <property name="age" value="40"/>
    </bean>

    <bean id="sally" class="Employee">                    
        <property name="name" value="Sally"/>
        <property name="age" value="44"/>
    </bean>
    </beans>

理想情况下,我希望employeemanager.java中的employeelist变量被注入一个包含bob和sally的employee对象的列表。运行应用程序时,出现以下错误:

Error creating bean with name 'employeeManager': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'employeeList' available

我不确定我是否遗漏了什么,或者我的设置是否不正确。

2j4z5cfb

2j4z5cfb1#

在applicationcontex.xml中声明bean的语法错误。声明bean的语法是
语法:

<beans>
    <bean id="<beanReferenceName>" class="fullyQualifiedName of the class"/>
</beans>

鸡蛋:

<beans>
    <bean id="transferService" class="com.acme.TransferServiceImpl"/>
</beans>

在applicationcontext.xml中添加employee类的完全限定名

相关问题