public class ClientService { private static ClientService clientService = new ClientService(); private ClientService() {} public static ClientService createInstance() { return clientService; }}
<bean id="clientService" class="examples.ClientService" factory-method="createInstance"/>
public class DefaultServiceLocator { private static ClientService clientService = new ClientServiceImpl(); private DefaultServiceLocator() {} public ClientService createClientServiceInstance() { return clientService; }}
<!-- 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 static AccountService accountService = new AccountServiceImpl(); private DefaultServiceLocator() {} public ClientService createClientServiceInstance() { return clientService; } public AccountService createAccountServiceInstance() { return accountService; }}
<bean id="serviceLocator" class="examples.DefaultServiceLocator"> <!-- inject any dependencies required by this locator bean --></bean><bean id="clientService" factory-bean="serviceLocator" factory-method="createClientServiceInstance"/><bean id="accountService" factory-bean="serviceLocator" factory-method="createAccountServiceInstance"/>
package org.hzy.entity;public class DefaultServiceLocator {private static CreateBean cre=new CreateBean();private static HelloSpring hs=new HelloSpring();private DefaultServiceLocator() {} public CreateBean createClientServiceInstance() { return cre; } public HelloSpring createHello(String a,Integer b) { return new HelloSpring(a,b);}}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"default-lazy-init="false"><!--<bean id="basehello" class="org.hzy.entity.BaseHello"><property name="xx"><value>sdfd</value></property></bean><bean id="hello1" class="org.hzy.entity.HelloSpring" parent="basehello"><property name="uage"><value>50</value></property></bean>--><!--<bean id="hello2" class="org.hzy.entity.HelloSpring"><constructor-arg index="0" type="java.lang.String" value="aa"/><constructor-arg index="1" type="java.lang.Integer" value="22"/></bean>--><!--<bean id="ch1" class="org.hzy.entity.Chinese"><property name="axe"><ref local="SteelAxe"/></property></bean>--><!--<bean id="ch2" class="org.hzy.entity.Chinese"><constructor-arg index="0" type="org.hzy.entity.Axe" ref="SteelAxe"></constructor-arg></bean><bean id="SteelAxe" class="org.hzy.entity.SteelAxe"></bean><bean id="StoneAxe" class="org.hzy.entity.StoneAxe"></bean>--><bean id="create" class="org.hzy.entity.CreateBean" factory-method="createInstance"></bean><bean id="serviceLocator" class="org.hzy.entity.DefaultServiceLocator"></bean><bean id="def" factory-bean="serviceLocator" factory-method="createClientServiceInstance"></bean><bean id="hs" factory-bean="serviceLocator" factory-method="createHello"><!--<property name="uname"><value>132</value></property><property name="uage"><value>132</value></property>--><constructor-arg><value>a.b.c.User</value></constructor-arg><constructor-arg><value>26</value></constructor-arg></bean></beans>
public static void main(String[] args) {ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");//CreateBean df=context.getBean("def",CreateBean.class);//System.out.println(df);HelloSpring hs=(HelloSpring) context.getBean("hs");hs.print1();}