欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

02Spring基于xml的IOC配置--实例化Bean的三种方式

程序员文章站 2023-10-29 12:48:22
maven依赖 org.springframework spring-context 5.0.6.RELEA ......

maven依赖

    <dependencies>
        <!--ioc相关依赖-->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-context</artifactid>
            <version>5.0.6.release</version>
        </dependency>
        <dependency>
            <groupid>junit</groupid>
            <artifactid>junit</artifactid>
            <version>4.12</version>
        </dependency>
    </dependencies>

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"
       xsi:schemalocation="http://www.springframework.org/schema/beans
                  http://www.springframework.org/schema/beans/spring-beans.xsd">



</beans>

 

1、使用默认无参构造函数实例化bean

1.1 创建student实体类

public class student {
    private string name;
    private integer age;

    
    public void say() {
        system.out.println("i'm a student");
    }

    public string getname() {
        return name;
    }

    public void setname(string name) {
        this.name = name;
    }

    public integer getage() {
        return age;
    }

    public void setage(integer age) {
        this.age = age;
    }
}

1.2 在applicationcontext.xml文件中装配student对象

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
       xsi:schemalocation="http://www.springframework.org/schema/beans
                  http://www.springframework.org/schema/beans/spring-beans.xsd">


    <!-- 装配studnt对象到ioc容器中 -->
    <bean id="student" class="com.demo.domain.student"/>

</beans>

1.3 测试

    private applicationcontext ac;

    @before
    public void init() {
        //加载配置文件
        ac = new classpathxmlapplicationcontext("applicationcontext.xml");
    }


    @test
    public void getstudentobjectfromsrpingioc() {
        //1. 根据bean的id去ioc容器中获取student对象
        student student = (student) ac.getbean("student");
        //2. say
        student.say();
    }

02Spring基于xml的IOC配置--实例化Bean的三种方式

 

 

 2. 使用静态工厂方法实例化bean

2.1 创建teacher实体类

public class teacher {

    public void say() {
        system.out.println("i'm a teacher");
    }
}

2.2 创建静态工厂类

public class staticfactory {

    /**
     * 用于创建teacher对象
     * @return
     */
    public static teacher createteacher() {
        return new teacher();
    }
}

 

2.3 使用静态工厂方法装配teacher对象

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
       xsi:schemalocation="http://www.springframework.org/schema/beans
                  http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 装配studnt对象到ioc容器中 -->
    <bean id="student" class="com.demo.domain.student"/>

    <!-- 使用静态工厂方法将teacher对象装配到ioc容器中 -->
    <bean id="teacher" class="com.demo.factory.staticfactory" factory-method="createteacher"/>


</beans>

2.4 测试

    @test
    public void getteaccherobjectfromsrpingioc() {
        //1. 根据bean的id去ioc容器中获取teacher对象
        teacher teacher = (teacher) ac.getbean("teacher");
        //2. say
        teacher.say();
    }

02Spring基于xml的IOC配置--实例化Bean的三种方式

3、使用实例工厂方法实例化bean

3.1 创建cat实体类

public class cat {
    public void say() {
        system.out.println("i'm a cat");
    }
}

3.2 创建实例工厂类

public class instancefactory {

    /**
     * 用于创建cat对象
     * @return
     */
    public cat createcat() {
        return new cat();
    }
}

3.3 使用实例工厂创建cat对象

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
       xsi:schemalocation="http://www.springframework.org/schema/beans
                  http://www.springframework.org/schema/beans/spring-beans.xsd">


    <!-- 装配studnt对象到ioc容器中 -->
    <bean id="student" class="com.demo.domain.student"/>

    <!-- 使用静态工厂方法将teacher对象装配到ioc容器中 -->
    <bean id="teacher" class="com.demo.factory.staticfactory" factory-method="createteacher"/>

    <!-- 使用实例工厂方法实例化bean -->
    <!-- 1. 装配实例工厂-->
    <bean id="instancefactory" class="com.demo.factory.instancefactory"/>
    <!-- 2. 使用实例工厂创建cat对象-->
    <bean id="cat" factory-bean="instancefactory" factory-method="createcat"/>

</beans>

3.4 测试

    @test
    public void getcatobjectfromsrpingioc() {
        //1. 根据bean的id去ioc容器中获取cat对象
        cat cat = (cat) ac.getbean("cat");
        //2. say
        cat.say();
    }

02Spring基于xml的IOC配置--实例化Bean的三种方式

4、小结

4.1 默认无参构造实例化bean

  spring的ioc在使用bean标签中的class属性配置的类的全限定类名通过反射创建对象,反射时默认调用的是类无参构造方法来实例化该对象的。

4.2 静态工厂方法实例化bean

  应用场景:静态工厂方法实例化bean的目的是为了将一个由静态方法获取的对象装配到ioc容器中。

  在spring的配置文件中配置bean标签,通过factory-method来引用获取静态方法返回的对象并装配到spring的ioc容器中.

4.3 实例工厂方法实例化bean

  应用场景:是为了将一个由非静态方法返回的对象装配到ioc容器中。

  1、 通过将实例工厂作为一个bean装配到ioc容器中

  2、 通过配置一个新的bean,使用factory-bean引用实例工厂这个bean,使用factory-method引用其中的非静态方法获取其返回的对象并将其装配到ioc容器中。