4

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> 
1
  • It's a bit of a weird thing to do, but you could make use of PropertyPlaceholderConfigurer. Something like this: forum.springsource.org/… Commented Sep 29, 2011 at 22:25

2 Answers 2

7

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 } 
Sign up to request clarification or add additional context in comments.

4 Comments

var1 and var2 values should be passed from Main class. Those are not constant values. Can I configure that in applicationContext.xml file?
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?
"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);
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);
0

As Nicholas says, it's a bit of a weird thing to do, but you could achieve it using PropertyPlaceholderConfigurer.

Something like this:

 public static void main(String args[]) { PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer(); Properties properties = new Properties(); properties.setProperty("var1", Integer.toString(var1)); properties.setProperty("var2", Integer.toString(var2)); configurer.setProperties(properties); FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(); context.addBeanFactoryPostProcessor(configurer); context.setConfigLocation("applicationContext.xml"); context.refresh(); Test test = (Test) context.getBean("test"); } 

NOTE: var1 and var2 should be static if you want to reference them from main()

Then reference the placeholders like so:

<bean id="test" class="com.path.test"> <constructor-arg index="0" type="int" value="${var1}"/> <constructor-arg index="1" type="int" value="${var2}"/> </bean> 

See also this forum thread: http://forum.springsource.org/showthread.php?71815-Passing-Bean-properties-using-java.util.Properties

API doc: http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html

HTH

3 Comments

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.
Well, I think PropertyPlaceholderConfigurer is only meant to be used once per ApplicationContex. So, maybe try another approach!
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/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.