1.使用类构造器实例化
1
|
<bean id=“orderService" class="cn.itcast.OrderServiceBean"/>
|
2.使用静态工厂方法实例化
1
2
3
4
5
6
7
8
9
10
|
<bean id="personService" class="cn.itcast.service.OrderFactory" factory-method="createOrder"/>
public class OrderFactory {
public static OrderServiceBean createOrder(){
return new OrderServiceBean();
}
}
|
PS:其中class是工厂类, factory-method是生成你所需bean的工厂方法。
示例:
工厂类:
1
2
3
4
5
6
7
8
9
10
11
|
package zmc;
public class PersobnSevicebeanFactory {
public static PersonServicebean createOrder(){
return new PersonServicebean();
}
}
|
配置文件:
1
|
<bean id="personService2" class="zmc.PersobnSevicebeanFactory" factory-method="createOrder"></bean>
|
3.使用实例工厂方法实例化:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<bean id="personServiceFactory" class="cn.itcast.service.OrderFactory"/>
<bean id="personService" factory-bean="personServiceFactory" factory-method="createOrder"/>
public class OrderFactory {
public OrderServiceBean createOrder(){
return new OrderServiceBean();
}
}
|
PS:此时要先实例化工厂类,再使用此工厂类的工厂方法来创建所需要的bean,其中factory-bean为工厂bean的名字(id)。
示例:
工厂类:
1
2
3
4
5
6
7
8
9
10
11
|
package zmc;
public class PersobnSevicebeanFactory {
public PersonServicebean createOrder2(){
return new PersonServicebean();
}
}
|
配置文件:
1
2
3
|
<bean id="personServiceFactory" class="zmc.PersobnSevicebeanFactory"></bean>
<bean id="personService3" factory-bean="personServiceFactory" factory-method="createOrder2"></bean>
|