Spring实例化Bean的三种方法

认识Bean

Spring容器可以被看作一个大工厂,而Spring容器中的Bean就相当于该工厂的产品。如果希望这个大工厂能够生产和管理Bean,这是则需要告诉容器需要哪些Bean,以及需要何种方式将这些Bean装配到一起
xml配置文件
Spring配置文件支持两种不同的格式,分别是xml文件格式和Properties文件格式。
xml格式配置文件的根元素是,该元素包含了多个子元素,每一个子元素定义了一个Bean,并描述了该Bean如何被装配到Spring容器中。
关于bean
定义Bean的示例代码如下所示:

<?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-3.2.xsd">
    <!-- 使用id属性定义person1,其对应的实现类为com.mengma.person1 -->
    <bean id="person1" class="damain.Person1" />
    <!--使用name属性定义person2,其对应的实现类为com.mengma.domain.Person2-->
    <bean name="Person2" class="domain.Person2"/>
</beans>

上述代码分别使用id和name属性定义了两个Bean,并使用class元素指定了Bean对应的实现类。
元素中包含很多属性,其常用属性如下表所示
在这里插入图片描述

实例化bean的三种方法

在面向对象程序中,要想调用某个类的成员方法,就需要先实例化该类的对象。在Spring总,实例化Bean有三种方式,分别是构造器实例化,静态工厂实例化和实例化工厂方式实例化。

构造器实例化

构造器实例化是指Spring容器通过对Bean对应的类中默认构造函数实例化Bean.
创建实体类Person1

package instance.constructor;
public class Person1 {
}

创建Spring配置文件applicationContext.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-3.2.xsd">
    <bean id="person1" class="instance.constructor.Person1" />
</beans>

在上述配置中,定义了一个id为person1的Bean,其中class属性指定了其对应的类为Person1.
创建测试类

package consturctor;

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

public class InstanceTest1 {
    @Test
    public void test(){
        //定义Spring配置文件的路径
        String xmlPath= "consturctor/applicationContext.xml";
        //初始化Spring容器,加载配置文件,并对bean进行实例化
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath);
        //通过容器获取id为person1的实例
        System.out.println(applicationContext.getBean("person1"));
    }
}

上述文件中,首先在test()方法中定义了Spring配置文件的路径,然后Spring容器中会加载配置文件。在加载的同时,Spring容器会通过实现类Person1中默认的无参构造函数对Bean进行实例化

静态工厂方式实例化

创建MyBeanFactory的类,并在该类中创建一个名为creteBean()的静态方法,用于创建Bean的实例,如下所示。

package static_factory;

public class MyBeanFactory {
    public static Person2 createBean(){
        return new Person2();
    }
}

创建配置文件后创建测试类

public class InstanceTest2 {
    @Test
    public void test(){
        //定义Spring配置文件的路径
        String xmlPath="static_factory/applicationContext.xml";
        //初始化Spring容器,加载配置文件,实例化bean
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath);
        //通过容器获取id为person2的实例
        System.out.println(applicationContext.getBean("person2"));
    }
}

实例工厂实例化

在Spring中,还有一种实例化Bean的方式就是采用实例工厂。在这种方式中,工厂类不再使用静态方法创建Bean的实例,而是直接在成员方法中创建Bean的实例
创建实体类

package factory;

public class Person3 {

}

创建实例工厂类

package factory;

public class MyBeanFactory {
    public MyBeanFactory(){
        System.out.println("person3工厂实例化中");
    }
    public Person3 createBean(){
        return new Person3();
    }
}

创建Spring配置文件

<?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-3.2.xsd">
    <!-- 配置实例工厂 -->
    <bean id="myBeanFactory" class="factory.MyBeanFactory" />
    <!-- factory-bean属性指定一个实例工厂,factory-method属性确定使用工厂中的哪个方法 -->
    <bean id="person3" factory-bean="myBeanFactory" factory-method="createBean" />
</beans>

创建测试类

public class InstanceTest3 {
    @Test
    public void test(){
        //定义spring配置文件路径
        String xmlPath="factory/applicationContext.xml";
        //初始化Spring容器,加载配置文件,实例化bean
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath);
        //通过容器获取id为person3的实例
        System.out.println(applicationContext.getBean("person3"));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值