Spring(五)之创建工厂

Spring创建静态工厂

我们创建一个猫类

public class Cat {
    private String name;
    private int age;

    public Cat(String name,int age){
        this.name=name;
        this.age=age;
    }
    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

创建一个生产猫的静态工厂

public class CatStaticFactory {
    public static Cat catFactory(int type){
        if(type==1){
            return new Cat("小花",6);
        }else if(type==2){
            return new Cat("小黑",7);
        }else {
            return new Cat("小黄",5);
        }
    }
}

在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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--创建静态工厂-->
    <bean id="catFactory" class="a02Factory.CatStaticFactory" factory-method="catFactory" >
        <constructor-arg name="type" value="1"></constructor-arg>
    </bean>
</beans>

此时在主程序中调用该方法就ok了!

    public static void main(String[] args) {
        //当前应用的xml配置文件在类路径下
        ApplicationContext ioc=new ClassPathXmlApplicationContext("ioc2.xml");
        //获取当前容器中id名为person1的对象
        Cat p=(Cat)ioc.getBean("catFactory");
        System.out.println(p);
    }

Spring创建实例工厂

创建一个生产猫的实例工厂

public class CatStaticFactory {
    public static Cat catFactory(int type){
        if(type==1){
            return new Cat("小花",6);
        }else if(type==2){
            return new Cat("小黑",7);
        }else {
            return new Cat("小黄",5);
        }
    }
}

在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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!--创建实例工厂-->
    <bean id="catFactory2" class="a02Factory.CatFactory"></bean>
    <bean id="cat" class="a02Factory.Cat" factory-bean="catFactory2" factory-method="catFactory">
        <constructor-arg name="type" value="1"></constructor-arg>
    </bean>
</beans>

此时在主程序中调用该方法就ok了!

public static void main(String[] args) {
        //当前应用的xml配置文件在类路径下
        ApplicationContext ioc=new ClassPathXmlApplicationContext("ioc2.xml");
        Cat p=(Cat)ioc.getBean("cat");
        System.out.println(p);
    }

FactoryBean内置工厂接口

继承FactoryBean接口

public class CatFactorySpring implements FactoryBean<Cat> {
    @Override
    public Cat getObject() throws Exception {
        return new Cat("小花",1);
    }

    @Override
    public Class<?> getObjectType() {
        return Cat.class;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }
}

在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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	    <!--FactoryBean是Spring定义的一个接口,只要是这个接口实现类,Spring都认为是个工厂-->
    <bean id="catBeanFactory" class="a02Factory.CatFactorySpring"></bean>
</beans>

此时在主程序中调用该方法就ok了!

public static void main(String[] args) {
        //当前应用的xml配置文件在类路径下
        ApplicationContext ioc=new ClassPathXmlApplicationContext("ioc2.xml");
        Cat p=(Cat)ioc.getBean("catBeanFactory");
        System.out.println(p);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值