Spring - what's wrong with my Autowiring?

I have this main spring application where the value of my field @Autowired

is zero in the output. What's 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>

      

The class for the APP execution:

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"));



   }
}

      

Output:

Your Message Hello World
autowired: null
Hello World

      

+3


source to share


3 answers


You need to add <context:annotation-config/>

to your spring.xml file that will validate 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>

      

+2


source


You need to set up the context to allow auto installation. See Answer:



how to configure autowire in spring

+3


source


you want to define @Quatifier

@Autowired(required=true)
@Qualifier("pi")
Double pi;

      

0


source







All Articles