Spring创建Bean的三种方式

一、 使用构造器创建Bean的实例
在依赖注入已经叙述多种注入实例

二、使用静态工厂方法创建Bean
实例如下:

**静态工厂方法所产生的产品是该接口的实例**
package com.home.bean;
/**
 * 定义Beging接口,静态工厂方法所产生的产品是该接口的实例
 */
public interface Being {
    public void testBeing();
}

**下面为两个实现类,静态工厂将会产生这两个实现类的实例**
package com.home.bean;

public class Dog implements Being{
    private String msg;
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public void testBeing() {
        // TODO Auto-generated method stub
    System.out.println(msg+",狗吃骨头");
    }
}

package com.home.bean;

public class Cat implements Being{
    private String msg;
    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void testBeing() {
        // TODO Auto-generated method stub
        System.out.println(msg+",猫爱吃鱼");
    }

}

下面是工厂类,该静态方法getBeing()用于返回一个Being的实例
package com.home.bean;

public class BeingFactory {
    //返回Beging实例的静态工厂方法
    //type参数决定返回哪一个Beging类的实例
    public static Being getBeing(String type){
        //调用此静态方法的参数为dog,则返回Dog的实例
        if(type.equalsIgnoreCase("dog")){
            return new Dog();
        }else{
            return new Cat();
        }
    }
}
配置文件:
 <!-- 下面配置驱动Spring调用beingFactory的静态方法getBeging方法来创建bean
           该bean元素包含constructor-arg元素用于静态工厂方法的参数
     -->
     <!-- 1、class属性值不再是bean实例的实现类
          2、constructor-arg元素用于静态工厂方法的参数
          3、使用factory-method属性指定创建bean实例的静态工厂方法
      -->
     <bean id="dog" class="com.home.bean.BeingFactory" factory-method="getBeing">
        <!-- 配置静态工厂方法的参数 -->
        <constructor-arg value="dog" />
        <property name="msg" value="我是狗"/>
     </bean>
      <bean id="cat" class="com.home.bean.BeingFactory" factory-method="getBeing">
        <!-- 配置静态工厂方法的参数 -->
        <constructor-arg value="cat" />
        <property name="msg" value="我是猫"/>
     </bean>
测试类如下:
package spring;

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

import com.home.bean.Being;

public class springTest2 {
    public static void main(String[] args) {
         ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
         Being b1=ctx.getBean("dog", Being.class);
         Being b2=ctx.getBean("cat", Being.class);
         b1.testBeing();
         b2.testBeing();
         /**
          * 我是狗,狗吃骨头
                              我是猫,猫爱吃鱼
          */
    }
}

三、调用实例工厂方法创建bean
区别:
调用静态工厂类只需要使用工厂类即可,而调用实例工厂方法则需要工厂实例。配置静态工厂方法使用class指定静态工厂类,而配置实例工厂方法则使用factory-bean指定工厂实例。
实例如下:

package com.home.bean;
//实例工厂方法所产生的对象将实现此接口
public interface People {
    public String sayHello(String name);
    public String sayGoodBye(String name);
}

两个实现类:
package com.home.bean;

public class Chinese implements People{
    public String sayHello(String name) {
        // TODO Auto-generated method stub
        return name+",你好!";
    }
    public String sayGoodBye(String name) {
        // TODO Auto-generated method stub
        return name+",再见!";
    }

}

package com.home.bean;

public class American implements People{

    public String sayHello(String name) {
        // TODO Auto-generated method stub
        return name+",Hello!";
    }

    public String sayGoodBye(String name) {
        // TODO Auto-generated method stub
        return name+",Good Bye!";
    }

}

实例工厂类如下:
负责生产People对象的实例工厂
package com.home.bean;
public class PersonFactory {
    //获得People的实例工厂方法
    public People getPerson(String type){
        if(type.equals("chin")){
            return new Chinese();
        }else{
            return new American();
        }
    }
}

配置文件如下:
<!-- 配置实例化工厂 -->
    <!--配置工厂bean,该bean负责产生其他bean的实例  -->
    <bean id="personFactory" class="com.home.bean.PersonFactory"></bean>
    <!--  下面配置驱动Spring调用getPerson方法来创建bean-->
    <bean id="chinese" factory-bean="personFactory" factory-method="getPerson">
      <constructor-arg value="chin" />
    </bean>
     <bean id="american" factory-bean="personFactory" factory-method="getPerson">
      <constructor-arg value="ame" />
    </bean>
测试类如下:
package spring;

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

import com.home.bean.People;
public class springTest3 {
    public static void main(String[] args) {
         ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
         People p1=ctx.getBean("chinese",People.class);
         System.out.println(p1.sayHello("中国人"));;
         People p2=ctx.getBean("american",People.class);
         System.out.println(p2.sayHello("American"));
         /**print:
          * 中国人,你好!
            American,Hello!
          */
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为你写诗_xue

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值