spring 中 静态工厂与实例工厂
程序员文章站
2022-05-21 23:06:16
...
静态工厂与实例工厂如下:
applicationContext.xml
<!-- 静态工厂 -->
<bean id="person1" class="demo.PersonFactory" factory-method="creatPerson1"></bean>
<!-- 实例工厂 -->
<bean id="personFactory" class="demo.PersonFactory"></bean>
<bean id="person2" factory-bean="personFactory" factory-method="createPerson2"></bean>
PersonFactory.java
public class PersonFactory {
/**
* 静态工厂方法
* @return
*/
public static Person creatPerson1(){
System.out.println("静态工厂创建Person...");
return new Person();
}
/**
* 实例工厂方法
* @return
*/
public Person createPerson2(){
System.out.println("实例工厂创建Person...");
return new Person();
}
}