当我使用bean post处理器时出现异常

时间:2018-02-08 07:07:18

标签: java spring

当我使用它时:

<bean id="circle" class="com.sameer.Learning.Circle" />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean>

在我的spring.xml中我得到运行时异常:

org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:667)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)

bean没有被创建。但是当我使用时:

<context:annotation-config></context:annotation-config>

bean已创建,我没有任何异常。 谁能告诉我为什么?

春天 - 4.3.7 JDK - 1.8

已编辑 - 已添加spring.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" 
 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-4.0.xsd">

<!-- <bean id="triangle" class="com.sameer.Learning.Triangle">
    <property name="pointA" ref="pointA" />
    <property name="pointB" ref="pointB" />
    <property name="pointC" ref="pointC" />


</bean> -->
<bean id="pointA" class="com.sameer.Learning.Point">
    <qualifier value="circleRelated"></qualifier>
    <property name="x" value="0"></property>
    <property name="y" value="0"></property>

</bean>

<bean id="pointB" class="com.sameer.Learning.Point">
    <property name="x" value="-20"></property>
    <property name="y" value="0"></property>
</bean>

<bean id="pointC" class="com.sameer.Learning.Point">
    <property name="x" value="20"></property>
    <property name="y" value="0"></property>
</bean>


<bean id="circle" class="com.sameer.Learning.Circle" />
<bean
    class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean>

以上是我用于注入依赖关系的spring .xml文件 下面是我想要注入对象的代码 代码 -

  package com.sameer.Learning;



    import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Required;
      import org.springframework.context.MessageSource;
      import org.springframework.beans.factory.annotation.*;
       public class Circle implements Shape {


    private Point center;

    @Autowired
    private MessageSource messageSource;


    public MessageSource getMessageSource() {
        return messageSource;
    }


    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    public Point getCenter() {
        return center;
    }

    @Autowired
    @Qualifier("circleRelated")
    public void setCenter(Point center) {
        this.center = center;
    }

    public void draw() {

        System.out.println("Point is " + center.getX() + "," +center.getY());
        //System.out.println(this.messageSource.getMessage("greeting",null,"Default",null));
}
}

1 个答案:

答案 0 :(得分:0)

您的bean Circle具有自动连接的依赖项MessageSourcePoint。所以,你应该手动将它传递给xml文件:

<bean id="circle" class="com.sameer.Learning.Circle" >
    <property name="messageSource" ref="..." />
    <property name="center" ref="..." />
</bean>

或添加<context:annotation-config></context:annotation-config>

  

context:annotation-config 用于在应用程序上下文中已经注册的bean中激活已应用的注释。

添加此注释后,Spring“看到”您的注释@Autowired并可以自动从应用程序上下文中注入所需的依赖项。

看一个类似的问题 - Difference between context:annotation-config vs context:component-scan