Spring-IOC创建对象的三种方式

2019-10-17  本文已影响0人  HeloWxl

Spring容器创建对象的三种方式

1、新建工程

项目工程结构.png

1.2 pom.xml

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

2、默认的构造方法

2.1 HelloIoc

public class HelloIoc {
    public void sayHello(){
        System.out.println("Hello Spring IOC");
    }
}

2.2 SpringIoc.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.0.xsd">

        <!--利用无参构造构造器-->
        <!--
                创建对象的第一种方式:利用无参构造构造器
                id:唯一的标识符
                class:类的全类名
        -->
        <bean id="helloIoc" class="com.ustcinfo.User"></bean>

         <!--别名 属性 name : 和 bean 的id 属性对应-->
        <alias name="helloIoc" alias="helloIoc2"/>
</beans>

2.3 测试

    @Test
    public void TestHelloIoc(){
        //从spring容器获得 //1 获得容器
        String xmlPath="bean.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);

        //2获得内容 --不需要自己new,都是从spring容器获得
        HelloIoc helloIoc = (HelloIoc) applicationContext.getBean("helloIoc");
        helloIoc.sayHello();
        //利用配置文件 alias 别名属性创建对象
        HelloIoc helloIoc2 = (HelloIoc) applicationContext.getBean("helloIoc2");
        helloIoc2.sayHello();
    }
无参构造.png

3、静态工厂方法

3.1 HelloStaticFactory

public class HelloStaticFactory {
   public HelloStaticFactory(){
       System.out.println("HelloStaticFactory constructor");
   }
   //静态工厂方法
   public static HelloIoc getInstances(){
       return new HelloIoc();
   }
}

3.2 bean.xml

    <!--
        创建对象的第二种方式:利用静态工厂方法
        factory-method:静态工厂类的获取对象的静态方法
        class:静态工厂类的全类名
      -->
    <bean id="helloStaticFactory" factory-method="getInstances" class="com.spring.HelloStaticFactory"></bean>

3.3 测试

  @Test
    public void TestHelloIoc2(){
        //从spring容器获得 //1 获得容器
        String xmlPath="bean.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);

        //2获得内容 --不需要自己new,都是从spring容器获得
        HelloIoc helloFactoryIoc = (HelloIoc) applicationContext.getBean("helloStaticFactory");
        helloFactoryIoc.sayHello();
    }

3.4 测试结果:

image.png
注意:spring容器只负责调用静态工厂方法,而这个静态工厂方法内部实现由程序员完成

4、实例工厂方法

4.1 HelloInstanceFactory

public class HelloInstanceFactory {
    public HelloInstanceFactory(){
        System.out.println("实例工厂方法构造函数");
    }
    //利用实例工厂方法创建对象
    public HelloIoc getInstance(){
        HelloIoc instanceIoc = new HelloIoc();
        return instanceIoc;
    }
}

4.2 bean.xml

    <!--
        创建对象的第三种方式:利用实例工厂方法
        factory-bean:指定当前Spring中包含工厂方法的beanID
        factory-method:工厂方法名称
      -->
    <bean id="instanceFactory" class="com.spring.HelloInstanceFactory"></bean>
    <bean id="instance" factory-bean="instanceFactory" factory-method="getInstance"></bean>

4.3 测试方法

 @Test
    public void TestHelloIoc3(){
        //从spring容器获得 //1 获得容器
        String xmlPath="bean.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);

        //2获得内容 --不需要自己new,都是从spring容器获得
        HelloIoc helloFactoryIoc = (HelloIoc) applicationContext.getBean("instance");
        helloFactoryIoc.sayHello();
    }

4.4 测试结果

image.png
上一篇下一篇

猜你喜欢

热点阅读