3

I've this basic spring application in which the value of my @Autowired field is null in output. What is wrong here?

package com.spring;

import org.springframework.beans.factory.annotation.Autowired;

public class HelloWorld {
   private String message;

   @Autowired
   private Double pi;

   public HelloWorld(String message){
       this.message  = message;
   }

   public void setMessage(String message){
      this.message  = message;
   }

   public void getMessage(){
      System.out.println("Your Message : " + message);
      System.out.println("autowired: "+pi);
   }
}

spring.xml config file.

<?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.0.xsd">

    <bean id="pi" name="pi" class="java.lang.Double" autowire="byName">
        <constructor-arg value="3.14"/>
    </bean>


   <bean id="helloWorld" class="com.spring.HelloWorld">
        <constructor-arg ref="msg" />
   </bean>

    <bean id="msg" class="java.lang.String" >
        <constructor-arg value="Hello World"/>
    </bean>     

</beans>

Class to execute the APP:

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
      HelloWorld hw =  context.getBean(HelloWorld.class);
      hw.getMessage();
      System.out.println(context.getBean("msg"));



   }
}

The output is:

Your Message Hello World
autowired: null
Hello World
ares
  • 4,283
  • 6
  • 32
  • 63

3 Answers3

3

You need to configure the context to allow autowiring. See this answer:

how do I configure autowire in spring

Community
  • 1
  • 1
user1675642
  • 747
  • 2
  • 5
  • 15
2

You need to add <context:annotation-config/> in your spring.xml file which will scan for spring annotations in your java files. Hope this helps.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    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.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />

    <bean id="pi" name="pi" class="java.lang.Double">
        <constructor-arg value="3.14" />
    </bean>
    <bean id="helloWorld" class="com.clsa.test.HelloWorld">
        <constructor-arg ref="msg" />
    </bean>
    <bean id="msg" class="java.lang.String">
        <constructor-arg value="Hello World" />
    </bean>
</beans>
Kulin Soni
  • 74
  • 1
  • 5
  • 1
    As a note `` also worked which I tried after this answer's solution. So it answered my other question why it was working flawlessly in my Spring MVC app. – ares Jul 01 '15 at 14:07
0
you want to define @Quatifier

@Autowired(required=true)
@Qualifier("pi")
Double pi;
Midhun Pottammal
  • 1,107
  • 1
  • 11
  • 25