spring中的bean用new的 方式会实例化吗
1个回答
展开全部
1.使用类构造器实现实例化(bean的自身构造器)
1 <bean id = "orderService" class="cn.itcast.OrderServiceBean"/>
2.使用静态工厂方法实现实例化
1 <bean id = "personService" class = "cn.itcast.OrderFactory" factory-method = "createOrder"/>
2 public class OrderFactory{
3 private static OrderFactory orderFactory = new OrderFactory();
4 private OrderFactory();
5 public static OrderFactory createOrder(){
6 return OrderFactory;
7 }
8 }
3.使用实例化工厂方法实现实例化(通过别的实体的bean)
1 <bean id = "personServiceFactory" class = "cn.itcast.service.OrderFactory"/>
2 <bean id = "persionService" factory-bean = "personServiceFactory" foctory-method = "createOrder"/>
3 public class OrderFactory{
4 private static OrderService orderService = new OrderService();
5 private OrderFactory();
6 public static OrderService createOrderServiceBean{
7 return OrderService;
8 }
9
10 }
1 <bean id = "orderService" class="cn.itcast.OrderServiceBean"/>
2.使用静态工厂方法实现实例化
1 <bean id = "personService" class = "cn.itcast.OrderFactory" factory-method = "createOrder"/>
2 public class OrderFactory{
3 private static OrderFactory orderFactory = new OrderFactory();
4 private OrderFactory();
5 public static OrderFactory createOrder(){
6 return OrderFactory;
7 }
8 }
3.使用实例化工厂方法实现实例化(通过别的实体的bean)
1 <bean id = "personServiceFactory" class = "cn.itcast.service.OrderFactory"/>
2 <bean id = "persionService" factory-bean = "personServiceFactory" foctory-method = "createOrder"/>
3 public class OrderFactory{
4 private static OrderService orderService = new OrderService();
5 private OrderFactory();
6 public static OrderService createOrderServiceBean{
7 return OrderService;
8 }
9
10 }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询