पास तर्कों के वसंत-फीचर वसंत 3.2.11 में देखने-विधि does not काम का उपयोग करके कंस्ट्रक्टर के लिए, लेकिन वसंत संस्करण में काम कर रहा है 4.1.1
यहाँ कोड मैं इसे जांच के लिए इस्तेमाल कर रहे हैं:
इस इंटरफेस कारखाना है ...
package prueba;
public interface Factory {
Person createPersonWithDependencies(String name);
}
इस सेम हम चाहते हैं, वसंत द्वारा प्रबंधित किया जा करने के लिए 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;
}
}
इस प्रसिद्ध HelloWorld सेवा है:
package prueba;
public class HelloWorldService {
public String getGreeting(){
return "hello world";
}
}
यह एक उदाहरण सेवा फैक्टरी
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());
}
}
का उपयोग करता है यह मुख्य वर्ग है कि सभी
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();
}
}
और अंत में परीक्षण है वसंत कॉन्फ़िगरेशन फ़ाइल:
<?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>
उत्पादन का उपयोग कर वसंत 4.1.1
hello world I am Jhon
hello world I am Maria
hello world I am Jhon
बहुत अच्छी और अच्छी जानकारी। धन्यवाद –
अद्यतन: यह सुविधा अभी तक वसंत का हिस्सा नहीं है। – javadeveloper
क्या यह अब स्प्रिंग 4 में समर्थित है? –