利用静态工厂方法来配置bean

2018-12-21  本文已影响0人  小竹猫

创建实体类Person

public class Person {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("我是person的setter方法。。。");
        this.name = name;
    }

    public Person() {
        super();
        // TODO Auto-generated constructor stub
        System.out.println("我是person的构造器...");
    }
    
    
    
    public Person(String name) {
        super();
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + "]";
    }
    
    public void init(){
        System.out.println("我是初始化bean方法。");
    }
    
    public void destroy(){
        System.out.println("我是销毁方法。");
    }
    
}

再创建一个名为PersonFactoryBean的接口实现类

public class PersonFactoryBean implements FactoryBean<Person> {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    //当前工厂要生产的对象
    @Override
    public Person getObject() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("1");
        return new Person(name);
    }

    @Override
    public Class<?> getObjectType() {
        // TODO Auto-generated method stub
        System.out.println("2");
        return Person.class;
    }

    @Override
    public boolean isSingleton() {
        // TODO Auto-generated method stub
        System.out.println("3");
        return false;
    }
    
    //当前工厂要生产的对象
    
}

创建类来生产对象

public class InstanceFactory {
    /*
     * 类变量  static 修饰的变量. 类名.直接调用的
     * 
     * 实例(对象)变量
     * */
    
    private Map<String,Person>persons= null;
    
    public InstanceFactory(){
        persons = new HashMap<String,Person>();
        persons.put("haha",new Person("haha"));
        persons.put("xixi", new Person("xixi"));
    }
    
    public Person getPerson(String name){
        return persons.get(name);
    }
}
public class StaticFactory {
    private static Map<String,Person>persons=new HashMap<String,Person>();
    
    //返回对象.
    static{
        persons.put("tom", new Person("汤姆"));
        persons.put("jack", new Person("杰克"));
    }
    
    public static Person getInstance(String name){
        return persons.get(name);
    }
}

在beans.xml中配置bean

<!-- 静态工厂方法  来配置bean ... 配置的bean不是工厂本身   而是工厂产生的对象. 
        class 指向静态工厂
        factory-method 需要指向工厂方法
        constructor-arg 向工厂方法传参
    -->
    <bean id="tom" class="com.igeek.lesson11.StaticFactory" factory-method="getInstance">
        <constructor-arg value="jack"></constructor-arg>
    </bean>

    <bean id="instanceFactory" class="com.igeek.lesson11.InstanceFactory"></bean>
    <bean id="xixi" factory-bean="instanceFactory" factory-method="getPerson">
        <constructor-arg value="xixi"></constructor-arg>
    </bean>
    
    <!-- factoyBean -->
    <bean id="pp" class="com.igeek.lesson11.PersonFactoryBean">
        <property name="name" value="李小花"></property>
    </bean>
</beans>

在MainTest中获取bean

public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("com/igeek/lesson11/beans.xml");
        Person bean = (Person)ac.getBean("pp");
        System.out.println(bean);
    }

输出结果:

3
3
1
Person [name=李小花]
上一篇下一篇

猜你喜欢

热点阅读