zoukankan      html  css  js  c++  java
  • Spring实例化bean的三种方式

    在面向对象编程的过程中,要想调用某个类的成员方法,首先要实例化该类的成员变量。

    在Spring 中,实例化Bean有三种方式:

    1、构造器实例化;2、静态工厂方式实例化;3、实例化工厂方式实例化

    构造器实例化:Spring容器通过Bean对应的类中默认的构造器函数实例化Bean.

    1-1、创建一个实体类 Person1

    package com.mengma.instance.constructor;
    public class Person1 {
    }

    1-2、创建Spring配置文件,在 com.mengma.instance.constructor 包下创建 Spring 的配置文件 applicationContext.xml

      定义了一个 id 为 person1 的 Bean,其中 class 属性指定了其对应的类为 Person1

    <?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="com.mengma.instance.constructor.Person1" />

    </beans>

    1-3、创建测试类,在 com.mengma.instance.constructor 包下创建一个名为 InstanceTest1 的测试类

    package com.mengma.instance.constructor;
    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 = "com/mengma/instance/constructor/ApplicationContext.xml";
            // 初始化Spring容器,加载配置文件,并对bean进行实例化
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                    xmlPath);
            // 通过容器获取id为person1的实例
            System.out.println(applicationContext.getBean("person1"));
        }
    }

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

    静态工厂方式实例化

    在 Spring 中,也可以使用静态工厂的方式实例化 Bean。此种方式需要提供一个静态工厂方法创建 Bean 的实例。下面通过案例演示如何使用静态工厂方式实例化 Bean。

    1. 创建实体类

    在项目的 src 目录下创建一个名为 com.mengma.instance.static_factory 的包,并在该包下创建一个实体类 Person2,该类与 Person1 相同,不需要添加任何成员。

    2. 创建静态工厂类

    在 com.mengma.instance.static_factory 包下创建一个名为 MyBeanFactory 的类,并在该类中创建一个名为 createBean() 的静态方法,用于创建 Bean 的实例,如下所示。

    package com.mengma.instance.static_factory;
    public class MyBeanFactory {
        // 创建Bean实例的静态工厂方法
        public static Person2 createBean() {
            return new Person2();
        }
    }

    3. 创建 Spring 配置文件

    在 com.mengma.instance.static_factory 包下创建 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="person2" class="com.mengma.instance.static_factory.MyBeanFactory"
            factory-method="createBean" />
    </beans>

    上述代码中,定义了一个 id 为 person2 的 Bean,其中 class 属性指定了其对应的工厂实现类为 MyBeanFactory,而 factory-method 属性用于告诉 Spring 容器调用工厂类中的 createBean() 方法获取 Bean 的实例。

    4. 创建测试类

    在 com.mengma.instance.static_factory 包下创建一个名为 InstanceTest2 的测试类,编辑后如下所示。

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

    实例工厂方式实例化

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

    同时,在配置文件中,需要实例化的 Bean 也不是通过 class 属性直接指向其实例化的类,而是通过 factory-bean 属性配置一个实例工厂,然后使用 factory-method 属性确定使用工厂中的哪个方法。下面通过案例演示实例工厂方式的使用。

    1. 创建实体类

    在项目的 src 目录下创建一个名为 com.mengma.instance.factory 的包,在该包下创建一个 Person3 类,该类与 Person1 类相同,不需要添加任何成员。

    2. 创建实例工厂类

    在 com.mengma.instance.factory 包下创建一个名为 MyBeanFactory 的类,编辑后如下所示。

    package com.mengma.instance.factory;
    public class MyBeanFactory {
        public MyBeanFactory() {
            System.out.println("person3工厂实例化中");
        }
        // 创建Bean的方法
        public Person3 createBean() {
            return new Person3();
        }
    }

    上述代码中,使用默认无参的构造方法输出 person3 工厂实例化中语句,使用 createBean 成员方法创建 Bean 的实例。

    3. 创建 Spring 配置文件

    在 com.mengma.instance.factory 包下创建 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="myBeanFactory" class="com.mengma.instance.factory.MyBeanFactory" />
        <!-- factory-bean属性指定一个实例工厂,factory-method属性确定使用工厂中的哪个方法 -->
        <bean id="person3" factory-bean="myBeanFactory" factory-method="createBean" />
    </beans>

    上述代码中,首先配置了一个实例工厂 Bean,然后配置了需要实例化的 Bean。在 id 为 person3 的 Bean 中,使用 factory-bean 属性指定一个实例工厂,该属性值就是实例工厂的 id 属性值。使用 factory-method 属性确定使用工厂中的 createBean() 方法。

    4. 创建测试类

    在 com.mengma.instance.factory 包下创建一个名为 InstanceTest3 的测试类,编辑后如下所示。

    package com.mengma.instance.factory;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class InstanceTest3 {
        @Test
        public void test() {
            // 定义Spring配置文件的路径
            String xmlPath = "com/mengma/instance/factory/applicationContext.xml"; // 初始化Spring容器,加载配置文件,并对bean进行实例化
            // 初始化Spring容器,加载配置文件,并对bean进行实例化
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                    xmlPath);
            // 通过容器获取id为person3实例
            System.out.println(applicationContext.getBean("person3"));
        }
    }
  • 相关阅读:
    WGS84经纬度坐标与web墨卡托之间的转换【转】
    ArcGIS API for Javascript配置
    百度地图BMap API实例
    VS2010 Web项目需要缺少的Web组件才能加载
    单态模式
    对服务的操作
    根据子级ID获取其所有父级
    在DropDownList里显示多级分类
    jQuery给CheckBox添加事件
    FolderBrowserDialog使用
  • 原文地址:https://www.cnblogs.com/henrypaul/p/12521994.html
Copyright © 2011-2022 走看看