A constructor that accepts arguments. Defines as a regular object or spring bean?

Let's say that I have a class with a constructor that takes one or more arguments. Let it also be said that the arguments are expected to be some input from the user. I. The argument cannot be known at compile time or configuration time only at run time. Should I define my class as a spring bean prototype or just create it using the "new".

If I have to define it as a bean, how can I pass the arguments?

+7
source share
3 answers

This is problematic in Spring. If your class has no dependency on other beans, just create it with new . If you have a class depending on another Spring beans, but still you want to pass some arguments at runtime, Spring does not currently support it.

However, see SPR-7431 and my article on passing a custom argument <lookup-methods/> . If all goes well, this feature should be part of Spring 3.2 and meet your requirements. It basically allows you to create prototype -scoped beans, while still passing some constructor argument.

+7
source

If your class does not depend on other beans in your context, then no, you should not make it a bean - it makes no sense. Just use new .

The only convincing reason to make it a bean is that it depends on other beans, in which case a bean with a prototype is justified. However, if a class requires these run-time values ​​in its constructor, you cannot do this without waiting for the class to change, to implement them using some method instead of using the constructor.

+5
source

spring -feature of pass arguments for the constructor using the lookup method does not work in spring 3.2.11, but works in spring version 4.1.1

here is the code i used to check:

this is the factory interface ...

 package prueba; public interface Factory { Person createPersonWithDependencies(String name); } 

this is the bean we want to manage spring with by introducing helloWorldService ...

 package prueba; public class Person { private HelloWorldService helloWorldService; public final void setHelloWorldService(HelloWorldService extraService) { this.helloWorldService = extraService; } public Person() { super(); } public Person(String name) { super(); this.name = name; } private String name; public final String sayHello() { return helloWorldService.getGreeting()+" I am "+name; } } 

this is the famous helloworld service:

 package prueba; public class HelloWorldService { public String getGreeting(){ return "hello world"; } } 

this is an example of a service that uses factory

 package prueba; public class Service { private Factory factory; public final void setFactory(Factory factory) { this.factory = factory; } public void doSomeThing(){ Person bean1= factory.createPersonWithDependencies("Jhon"); System.out.println(bean1.sayHello()); Person bean2= factory.createPersonWithDependencies("Maria"); System.out.println(bean2.sayHello()); System.out.println(bean1.sayHello()); } } 

this is the main class that checks everything

 package prueba; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestLookupMethodWithArguments { /** * Main method. */ public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContext.xml"); Service service=applicationContext.getBean("service",Service.class); service.doSomeThing(); } } 

and finally the spring configuration 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.xsd"> <bean id="helloWorldService" class="prueba.HelloWorldService" /> <bean id="Person" class="prueba.Person" scope="prototype"> <property name="helloWorldService" ref="helloWorldService" /> </bean> <bean id="myFactory" class="prueba.Factory"> <lookup-method name="createPersonWithDependencies" bean="Person" /> </bean> <bean id="service" class="prueba.Service"> <property name="factory" ref="myFactory" /> </bean> </beans> 

output using spring 4.1.1

 hello world I am Jhon hello world I am Maria hello world I am Jhon 
0
source

All Articles