使用实列工厂创建Bean

    使用实列工厂方法时,配置Bean的实列<bean>无须配置class属性,因为Spring容器不再直接实列化该Bean,Spring容器仅仅调用实列工厂的工厂方法,工厂方法负责创建Bean实列。

    下面先定义Person接口,PersonFactory工厂产生的所有对象都实现Person接口:

package com.custle.spring.inter;

/**
 * Created by admin on 2018/3/1.
 */
public interface Person {
    public String sayHello(String name);//定义一个打招呼的方法
    public String sayGoodBye(String name);//定义一个告别方法
}

     Person实列类Chinese:

package com.custle.spring.impl;

import com.custle.spring.inter.Person;

/**
 * Created by admin on 2018/3/1.
 */
public class Chinese implements Person {
    @Override
    public String sayHello(String name) {
        return name + ",您好!";
    }

    @Override
    public String sayGoodBye(String name) {
        return name + ",再见!";
    }
}

    Person实列类American:

package com.custle.spring.impl;

import com.custle.spring.inter.Person;

/**
 * Created by admin on 2018/3/1.
 */
public class American implements Person{
    @Override
    public String sayHello(String name) {
        return name + ",Hello!";
    }

    @Override
    public String sayGoodBye(String name) {
        return name + ",GoodBye!";
    }
}

    定义PersonFactory工厂:

package com.custle.spring.factory;

import com.custle.spring.impl.American;
import com.custle.spring.impl.Chinese;
import com.custle.spring.inter.Person;

/**
 * Created by admin on 2018/3/1.
 */
public class PersonFactory {
    public Person getPerson(String arg){
        if(arg.equals("chinese")){
            return new Chinese();
        }else{
            return new American();
        }
    }
}

    在XML中配置:

    <!--配置工厂Bean,该Bean负责产生其他Bean实列-->
	<bean id="personFactory" class="com.custle.spring.factory.PersonFactory"/>
	<!--采用实列工厂创建Bean实列,factory-bean指定对应的工厂Bean,factory-method指定工厂方法-->
	<bean id="chinese" factory-bean="personFactory" factory-method="getPerson">
		<!--工厂中getPerson方法参数注入-->
		<constructor-arg value="chinese"/>
	</bean>
	<bean id="american" factory-bean="personFactory" factory-method="getPerson">
		<!--工厂中getPerson方法参数注入-->
		<constructor-arg value="american"/>
	</bean>

    factory-bean指定了实列工厂Bean的Id,factory-method指定了实列工厂的方法,constructor-arg指定了工厂方法注入的参数;

    测试程序:

package com.custle.spring.test;

import com.custle.spring.inter.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by admin on 2018/3/1.
 */
public class PersonTest {
    public static void main(String[] args) {
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("applicationContext-spring.xml");
        Person chinese = applicationContext.getBean("chinese", Person.class);
        System.out.println(chinese.sayGoodBye("小明"));
        System.out.println(chinese.sayHello("小明"));
        Person american = applicationContext.getBean("american", Person.class);
        System.out.println(american.sayGoodBye("xiaoming"));
        System.out.println(american.sayHello("xiaoming"));
    }
}

    控制台输出:

小明,再见!
小明,您好!
xiaoming,GoodBye!
xiaoming,Hello!

    调用实列工厂方法创建Bean,与调用静态工厂创建Bean的用法基本相似。区别如下:

    调用实列工厂创建Bean,必须将实列工厂配置成Bean实列。而静态工厂方法创建Bean,则无须配置工厂Bean。

    调用实列工厂方法创建Bean,必须使用factory-bean属性确定工厂Bean。而静态工厂方法创建Bean,则使用class元素确定静态工厂类。

猜你喜欢

转载自my.oschina.net/u/3697923/blog/1626961