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 { 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
Jesus mc
source share