标签:
创建一个 Bean 实例对象的方法通常有如下方式:
XML 配置:
<bean id="exampleBean" class="examples.ExampleBean"/> <bean name="anotherExample" class="examples.ExampleBeanTwo"/>
XML 配置:
<bean id="clientService" class="examples.ClientService" factory-method="createInstance"/>
静态工厂类:
public class ClientService { private static ClientService clientService = new ClientService(); private ClientService() {} public static ClientService createInstance() { return clientService; } }
XML 配置:
<!-- the factory bean, which contains a method called createInstance() --> <bean id="serviceLocator" class="examples.DefaultServiceLocator"> <!-- inject any dependencies required by this locator bean --> </bean> <!-- the bean to be created via the factory bean --> <bean id="clientService" factory-bean="serviceLocator" factory-method="createClientServiceInstance"/>
实例工厂类:
public class DefaultServiceLocator { private static ClientService clientService = new ClientServiceImpl(); private DefaultServiceLocator() {} public ClientService createClientServiceInstance() { return clientService; } }
Spring(3.2.3) - Beans(3): Bean 实例的创建方式
标签:
原文地址:http://www.cnblogs.com/huey/p/4506884.html