使用实列工厂建立Bean

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

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

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:app

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:ide

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中配置:code

<!--配置工厂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指定了工厂方法注入的参数;xml

    测试程序:对象

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的用法基本类似。区别以下:get

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

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

相关文章
相关标签/搜索