Springframework constructor-arg

15,166

You can pass values in like this:

<bean id="test" class="com.path.test.Test">
   <constructor-arg index="0" type="int" value="123"/>
   <constructor-arg index="1" type="int" value="456"/>
</bean>

You should remember to put your fully-qualified class name as the value of the class attribute.

That said, your Test class is not holding onto its state. If you want to get a hold of the values you specified in your applicationContext.xml, you should create some members of Test:

public class Test {
    private int v1;
    private int v2;

    public Test (int var1, int var2) {v1 = var1; v2 = var2;}

    public int getVOne() {
        return v1;
    }

    public int getVTwo() {
        return v2;
    }
}

You should then be able to access these in your main method like this:

public static void main(String args[]) {

    ApplicationContext context = 
       new FileSystemXmlApplicationContext("applicationContext.xml");
    Test test = context.getBean("test");
    int v1 = test.getVOne();
    int v2 = test.getVTwo();

    System.out.println("V1: " + v1 + " V2: " + v2); //output: V1: 123 V2: 456
}
Share:
15,166

Related videos on Youtube

user12121
Author by

user12121

Updated on June 04, 2022

Comments

  • user12121
    user12121 almost 2 years

    I have a MainClass which have 2 variables. I would like to pass those 2 values to a springframework bean class "Test". how do I define that in applicationContext.xml and also how do I pass those 2 variable values to the bean "Test".

    Ex:

    class MainClass {
           public int var1;
          public int var2;
          public Test test;
    
       public void setVar1(int var11) {
        var1 = var11;
       }
    
        public void setVar2(int var22) {
            var2 = var22;
        }
    
       public static void main(String args[]) {
    
           ApplicationContext context = 
               new FileSystemXmlApplicationContext("applicationContext.xml");
          Test = context.getBean("test");
       }
      }
    

    ------------ TEST class ------------

    public class Test {
    
     public Test (int var1, int var2) {}
    }
    

    ------------- applicationContext.xml -------------

       <bean id="test" class="com.path.test">
           <constructor-arg index="0" type="int" value="????"/>
           <constructor-arg index="1" type="int" value="????"/>
       </bean>
    
  • user12121
    user12121 over 12 years
    var1 and var2 values should be passed from Main class. Those are not constant values. Can I configure that in applicationContext.xml file?
  • nicholas.hauschild
    nicholas.hauschild over 12 years
    That is not what you use Spring for. Dependency injection is about setting your dependencies up outside of code. What is it that you are trying to do?
  • user12121
    user12121 over 12 years
    "Main" class defines 2 variables. "Test" class is instantiated from "Main" class and should pass those 2 variables to "Test" class. How can I acheive this using Spring. For Ex: Main Class: int var1 = getValueFromDatabase(1); int var2 = getValueFromDatabase(2); Test test = new Test(var1, var2);
  • nicholas.hauschild
    nicholas.hauschild over 12 years
    I understand your example, but Spring is not necessary to create an instance of Test in this case. Here, you should create Test using the new operator just as you have shown: Test test = new Test(var1, var2);
  • user12121
    user12121 over 12 years
    Thank you very much. This is very helpful.
  • user12121
    user12121 over 12 years
    I am also using "PropertyPlaceHolders" in my applicationContext.xml file, which retrieves values from "multiple properties" files. When I used your approach, it is not taking the properites from applicationContext.xml file. Please help.
  • laher
    laher over 12 years
    Well, I think PropertyPlaceholderConfigurer is only meant to be used once per ApplicationContex. So, maybe try another approach!
  • laher
    laher over 12 years
    e.g. call if you're getting the values from the DB, the datasource should be set up in the applicationContext xml, and then you can use a MethodInvokingFactoryBean to invoke getValueFromDatabase(1) ... static.springsource.org/spring/docs/3.0.x/javadoc-api/org/…