2

I have a xml bean 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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   <context:annotation-config/>
   <bean id="helloWorld" class="com.a.b.HelloWorld"> 
         <property name="attr1" value="Attr1 from XML"></property>
   </bean>
    <bean id="helloWorld2" class="com.a.b.HelloWorld2">
        <property name="attr2" value="Attr2 from XML"></property>
    </bean>
</beans>

And I have use constructor autowiring like this

public class HelloWorld2{
       private String attr2;
       public void setAttr2(String message){
          this.attr2  = message;
       }

       public void getAttr2(){
          System.out.println("getAttr2 == " + attr2);
       }


    }

public class HelloWorld{
       private String attr1;
       private HelloWorld2 helloWorld2;    
       public HelloWorld(){

       }
       @Autowired
       public HelloWorld(HelloWorld2 helloWorld2){
           System.out.println("hhh");
           this.helloWorld2 = helloWorld2;
       }


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

       public void getAttr1(){
          System.out.println("getAttr1 == " + attr1);
       }
       public void getH(){
           helloWorld2.getAttr2();
       }
    }

And autowiring is working fine.

Now I want to move my beans to Configuation class. But then how to move the code so as autowiring works?

I have tried like this, but its not working

@Configuration
public class Config {
    @Bean
    public HelloWorld helloWorld(){
        HelloWorld a = new HelloWorld();
        a.setAttr1("Demo Attr1");
        return a;

    }

    @Bean
    public HelloWorld2 helloWorld2(){
        HelloWorld2 a = new HelloWorld2();
        a.setAttr2("Demo Attr2");
        return a;               
    }
}
Ankit Bansal
  • 2,162
  • 8
  • 42
  • 79
  • 1
    Possible duplicate of [Converting spring XML file to spring @Configuration class](http://stackoverflow.com/questions/24014919/converting-spring-xml-file-to-spring-configuration-class) – James Feb 17 '17 at 09:00
  • I think this is the way to provide the bean through Configuration what I know, autowiring the variable through passing the constructor parameter at @Autowired will be interesting... to know. – ArifMustafa May 03 '18 at 14:34

1 Answers1

5

I think what you want to achieve is the injection of a HelloWorld2 instance into the method that creates the HelloWorld @Bean?

This should do it:

@Configuration
public class Config {
    @Bean
    public HelloWorld helloWorld(HelloWorld2 helloWorld2){
        HelloWorld a = new HelloWorld(helloWorld2);
        a.setAttr1("Demo Attr1");
        return a;

    }

    @Bean
    public HelloWorld2 helloWorld2(){
        HelloWorld2 a = new HelloWorld2();
        a.setAttr2("Demo Attr2");
        return a;               
    }
}

This might be a duplication of these questions:

Community
  • 1
  • 1
James
  • 1,095
  • 7
  • 20
  • It works with slight modification. I also have to change HelloWorld a = new HelloWorld(); to HelloWorld a = new HelloWorld(helloWorld2); Is it the right way? – Ankit Bansal Feb 16 '17 at 18:34
  • But now this code is working even when i have removed @Autowired from the HelloWorld Constructor and O/p of obj1.getH() is giving me Demo Attr2. How is it happening? I mean how does HelloWorld gets the instance of HelloWorld2 without autowiring? – Ankit Bansal Feb 16 '17 at 18:54
  • You don't need the Autowired annotation if you are explicitly calling the constructor. – James Feb 16 '17 at 20:00