Employee2Factory.java:
package text.spring._05_instance.staticfactory;
/**
* Created by Administrator on 2019/9/3.
*/
public class Employee2Factory {
public static Employee2 getObject(){
System.out.println("静态工厂实例化对象");
return new Employee2();
}
}
App.java:
package text.spring._05_instance.staticfactory;
import text.spring._05_instance.constructor.Employee;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Created by Administrator on 2019/9/3.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class App {
@Autowired
private ApplicationContext context;
@Test
public void testOld(){
Employee2 employee2 = Employee2Factory.getObject();
System.out.println(employee2);
}
@Test
public void testSpring() throws Exception {
Employee2 employee2 = context.getBean(Employee2.class);
System.out.println(employee2);
}
}
App-context.xml:
<?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="employee2" class="text.spring._05_instance.staticfactory.Employee2Factory" factory-method="getObject">
</bean>
</beans>