mùa xuân-tính năng của đối số đường chuyền qua constructor sử dụng công tra cứu-method doesnt vào mùa xuân 3.2.11, nhưng đang làm việc trong phiên bản mùa xuân 4.1.1
đây là đoạn code tôi sử dụng để kiểm tra xem nó:
đây là nhà máy giao diện ...
package prueba;
public interface Factory {
Person createPersonWithDependencies(String name);
}
đây là đậu chúng ta muốn được quản lý bởi mùa xuân, tiêm 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;
}
}
này là dịch vụ helloworld nổi tiếng:
package prueba;
public class HelloWorldService {
public String getGreeting(){
return "hello world";
}
}
đây là một dịch vụ ví dụ sử dụng Nhà máy
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());
}
}
này là lớp chính mà kiểm tra tất cả các
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();
}
}
và cuối cùng tệp cấu hình mùa xuân:
<?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>
đầu ra sử dụng vào mùa xuân 4.1.1
hello world I am Jhon
hello world I am Maria
hello world I am Jhon
Thông tin rất tốt và tốt đẹp. Cảm ơn –
Cập nhật: Tính năng này KHÔNG phải là một phần của mùa xuân. – javadeveloper
có được hỗ trợ trong Spring 4 ngay bây giờ không? –