spring

静态工厂创建Bean

定义接口

public interface Being {
    public void testBeing();
}

接口实现

public class Dog implements Being {
    private String msg;

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void testBeing() {
        System.out.println(msg+",dogs like bones");
    }
}
public class Cat implements Being {
    private String msg;

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void testBeing() {
        System.out.println(msg+",cats like rats");
    }
}

静态工厂

public class BeingFactory {
    public static Being getBeing(String arg){
        if(arg.equalsIgnoreCase("dog")){
            return new Dog();
        }
        else{
            return new Cat();
        }
    }
}

Spring配置services.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dog" class="org.mythsky.springdemo1.BeingFactory" factory-method="getBeing">
        <constructor-arg value="dog"></constructor-arg>
        <property name="msg" value="this is a dog"></property>
    </bean>
    <bean id="cat" class="org.mythsky.springdemo1.BeingFactory" factory-method="getBeing">
        <constructor-arg value="cat"></constructor-arg>
        <property name="msg" value="this is a dog"></property>
    </bean>


</beans>

测试

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    public static void main(String[] args){
        ApplicationContext context=new ClassPathXmlApplicationContext("services.xml");
        Being b1=context.getBean("dog",Being.class);
        Being b2=context.getBean("cat",Being.class);
        b1.testBeing();
        b2.testBeing();
    }
}

结果

 实例工厂创建Bean

public interface Person {
    void sayHello(String name);
    void sayGoodbye(String name);
}
public class American implements Person {
    public void sayHello(String name) {
        System.out.println(name+",Hello!");
    }

    public void sayGoodbye(String name) {
        System.out.println(name+",Good Bye!");
    }
}
public class Chinese implements Person {
    public void sayHello(String name) {
        System.out.println(name+"你好!");
    }

    public void sayGoodbye(String name) {
        System.out.println(name+"再见!");
    }
}
public class PersonFactory {
    public Person getPerson(String country){
        if(country.equalsIgnoreCase("US")){
            return new American();
        }else{
            return new Chinese();
        }
    }
}
<bean id="personFactory" class="org.mythsky.springdemo1.PersonFactory"></bean>
    <bean  id="chinese" factory-bean="personFactory" factory-method="getPerson">
        <constructor-arg value="chinese"></constructor-arg>
    </bean>
    <bean  id="american" factory-bean="personFactory" factory-method="getPerson">
        <constructor-arg value="us"></constructor-arg>
    </bean>
Person p1=context.getBean("chinese",Person.class);
        Person p2=context.getBean("american",Person.class);
        String name="tom";
        p1.sayHello(name);
        p1.sayGoodbye(name);
        p2.sayHello(name);
        p2.sayGoodbye(name);

 spring默认是单例模式,如果每次需要新建的话,需要设置scope

Dog dog1=context.getBean("dog",Dog.class);
        Dog dog2=context.getBean("dog",Dog.class);
        System.out.println(dog1==dog2);

<bean id="dog" class="org.mythsky.springdemo1.BeingFactory" factory-method="getBeing" scope="prototype">
        <constructor-arg value="dog"></constructor-arg>
        <property name="msg" value="this is a dog"></property>
    </bean>

再次运行

 

posted @ 2017-11-17 09:49  uptothesky  阅读(277)  评论(0编辑  收藏  举报