为什么Spring基本@Autowired无法正常工作?

马西布

我是春季新人。但是尝试基本的东西。根据理论,我应该工作,但是没有工作。我有2个具有不同ID的相同豆。我应该能够@Autowired拥有唯一的id @Qualifier("PersonBean2")但它给错误。我不知道哪里出了什么问题。请通知我?谢谢!

配置springBeans.xml在哪里:-

 <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-2.5.xsd">

<bean    

 class="org.springframework.beans.factory.annotation.
  AutowiredAnnotationBeanPostProcessor"/>


  <bean id="customer" class="com.mkyong.common.Customer" >
    <property name="action" value="buy" />
    <property name="type" value="1" />
</bean>

<bean id="PersonBean1" class="com.mkyong.common.Person">
    <property name="name" value="mkyong1" />
    <property name="address" value="address 1" />
    <property name="age" value="28" />
</bean>

  <bean id="PersonBean2" class="com.mkyong.common.Person">
    <property name="name" value="mkyong2" />
    <property name="address" value="address 2" />
    <property name="age" value="28" />
    </bean>

   </beans>

Customer.java:-

public class Customer {

@Autowired
@Qualifier("PersonBean2")
private Person person;
private int type;
private String action;
}

Person.java:-

public class Person {
private String name;
private String address;
private int age;
}

所有二传手和消气剂均已删除。

测试app.java:-

public class App {
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext(
            "SpringBeans.xml");

    Customer cust = (Customer) context.getBean("customer");
    System.out.println(cust);
  }
}

错误:-


Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customer': Autowiring of fields failed; 
    nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mkyong.common.Person com.mkyong.common.Customer.person; 
    nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.mkyong.common.Person] is defined: expected single matching bean but found 2: [PersonBean1, PersonBean2]

然后马库斯
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

AutowiredAnnotationBeanPostProcessor顾名思义,您已添加了仅处理@Autowired注释的。代替直接使用处理器,添加<context:annotation-config />会添加其他处理器,这些处理器也要考虑@Qualifier在内。

<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:annotation-config />

另一个技巧是无spring-beans.xsd版本架构胜于版本化架构spring-beans-2.5.xsd

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么我的基本默认.acceptbutton无法正常工作?

来自分类Dev

Spring @Autowired无法正常工作并返回null

来自分类Dev

@Autowired无法正常工作

来自分类Dev

为什么此基本Node.js错误处理无法正常工作?

来自分类Dev

不确定为什么此基本watchOS 2应用无法正常工作

来自分类Dev

spring-boot-starter-web @Autowired无法正常工作

来自分类Dev

测试时Web Service中的Spring Autowired无法正常工作

来自分类Dev

Web Service中的Spring Autowired无法正常工作

来自分类Dev

(Spring-Batch)Autowired在StepScopeTestExecutionListener中无法正常工作

来自分类Dev

spring-boot-starter-web @Autowired无法正常工作

来自分类Dev

为什么jQuery代码无法正常工作?

来自分类Dev

推送无法正常工作-为什么?

来自分类Dev

为什么“ nextDateAfterDate”功能无法正常工作?

来自分类Dev

为什么我的__clone()无法正常工作?

来自分类Dev

为什么sizeof无法正常工作?

来自分类Dev

为什么我的ViewBag无法正常工作?

来自分类Dev

为什么我的DATEPART无法正常工作?

来自分类Dev

为什么Bxslider无法正常工作

来自分类Dev

为什么jQuery show()无法正常工作?

来自分类Dev

为什么to!int()无法正常工作?

来自分类Dev

SQL _通配符无法正常工作。为什么?

来自分类Dev

为什么DozerConverter无法正常工作?

来自分类Dev

为什么与零的比较无法正常工作?

来自分类Dev

为什么我的闹钟无法正常工作?

来自分类Dev

为什么会话消息无法正常工作?

来自分类Dev

为什么我的ButtonClickListerner无法正常工作?

来自分类Dev

为什么轮播悬停无法正常工作

来自分类Dev

为什么-replace无法正常工作?

来自分类Dev

为什么这种转换无法正常工作?

Related 相关文章

热门标签

归档