How to pass parameter(s) via constructor to another object dynamically with spring annotation

Let say we have a simple class like below. We could use it within/without default constructor. I am really curious about is it possible to pass argument/parameter to another object via constructor in Spring framework. To explain what I want to do, please see the code sample below.

@Component
public class Class{
    String text = null;
    String text2 = null;

    Class( text, text2 ){
        super();
        this.text = text;
        this.text2 = text2;
    }

    @Overide
    public void toString(){
        System.out.printf( "Text" + text + ", " + "Text2" + text2);
    }

    /** Methods and Setter/Getter etc. **/

}

After defining class and Spring annotations, I would like to call this object via Spring.

public class Usage{
    @Autowired
    Class classExample;

    public void method(){
        String text = "text";
        String text2 = "text2";


        /** One way can be using setters **/
        classExample.setText(text);
        classExample.setText2(text2);
        System.out.println( classExample.toString() );


        /** Another way can be using a method **/
        classExample.set(text, text2);
        System.out.println( classExample.toString() );



        /**What I wanted is calling it via constructor injection dynamically**/

        /** Normal way we could call this **/
        //classExample = new Class(text, text2);
        //System.out.println( classExample.toString() );

    }
}

Is it possible to inject parameter(s) dynamically to another object.

Answers


If you use spring xml configuration you can use the constructor-arg parameter.

<bean id="exampleBean" class="examples.ExampleBean">
   <constructor-arg type="int" value="7500000"/>
   <constructor-arg type="java.lang.String" value="42"/>
</bean>

http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-factory-collaborators

But remember, the default scope of your bean is singleton!

Is it possible to inject parameter(s) dynamically to another object.

Lets create a "dynamic" bean, so lets set the scope of the bean to prototype to get a fresh instance evrytime it gets called.

<bean id="exampleBean" class="examples.ExampleBean" scope="prototype">
   <constructor-arg type="int" value="#{getRandomNumber}"/>
</bean>

In this case, every time a new bean will be created with a new random number.


You should take a look at FactoryBean


Need Your Help

Page_Load(object sender, EventArgs e)---Why is "object" not capitalised?

c# .net

"object" here should refer to the "Object" class. As with all class identifiers in the .NET framework, it is supposed to be capitalised although not case sensitive. But I realise in c# it is not

Form Collection Error

php symfony symfony-forms

I am trying to take one form type and display it however times i need for the user to upload a patch upload at one time. So say 30 files to uploaded, 30 forms on the page. I am receiving this error...