0

When i use this :

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

in my spring.xml i am getting runtime exceptions :

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)

The bean doesnt get created. But when i used :

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

The bean was created and i did not get any exception. Can anybody please tell me why ?

Spring - 4.3.7 JDK - 1.8

Edited - Added 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>

'

The above is the spring .xml file i used to inject dependency The below is the code where i want to inject the object Code -

  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));
}
}
reactdontact
  • 53
  • 1
  • 10

1 Answers1

0

Your bean Circle has autowired dependencies MessageSource and Point. So, you should pass it into xml-file manually:

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

or add <context:annotation-config></context:annotation-config>.

context:annotation-config is used to activate applied annotations in already registered beans in application context.

After adding this annotation Spring "see" your annotations @Autowired and can automatically inject needed dependencies from application context.

Look a similar question - Difference between context:annotation-config vs context:component-scan

Nikolai
  • 760
  • 4
  • 9