美文网首页
Spring-IOC的发展

Spring-IOC的发展

作者: HeloWxl | 来源:发表于2019-12-14 00:14 被阅读0次

    SpringIoc的发展经历了三种方式:

    • 1.通过new的方式创建
    • 2.通过工厂模式获取不同的实例对象(需要自己写工厂)
    • 3.通过ioc容器获取不同的实例对象(只需要自己配置即可)

    1、目录结构

    目录结构.png

    1.1 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>
    
    

    1.2 User实体类

    package com.spring;
    /**
     * @Classname User
     * @Description TODO 用户实体类
     * @Date 2019/12/13 10:31 下午
     * @Created by wangxianlin
     */
    public class User {
    
        private String id;
        private String name;
        private String address;
        public void useCreateMethod(String name){
            System.out.println("使用"+name+"方式创建对象");
        }
        public String getId() {
            return id;
        }
        @Override
        public String toString() {
            return "User{" +
                    "id='" + id + '\'' +
                    ", name='" + name + '\'' +
                    ", address='" + address + '\'' +
                    '}';
        }
    }
    

    1.3 CreteUserFactory

    public class CreteUserFactory {
        public static User getUser(){
            return new User();
        }
    }
    

    1.4 bean.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-3.0.xsd">
    
        <bean id="User" class="com.spring.User"/>
    </beans>
    

    2、创建方式

    2.1 传统的new方式

      /***
         * @Author wangxl
         * @Description //TODO 通过new方式创建对象
         * @Date 11:33 下午 2019/12/13
         * @Param []
         * @return void
         **/
        @Test
        public void TestUser2(){
            User user = new User();
            user.setAddress("安徽省合肥市");
            user.setId("1238784");
            user.setName("HeloWxl");
            System.out.println(user.toString());
            user.useCreateMethod("new");
        }
    
    new.png

    2.2 通过工厂创建

        @Test
        public void TestUser3(){
            User user = CreteUserFactory.getUser();
            user.setAddress("安徽省合肥市");
            user.setId("1238784");
            user.setName("HeloWxl");
            System.out.println(user.toString());
            user.useCreateMethod("factory");
        }
    
    factory.png

    2.3 通过IOC创建

        @Test
        public void TestUser(){
            //从spring容器获得 //1 获得容器
            String xmlPath="bean.xml";
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
            //2获得内容 --不需要自己new,都是从spring容器获得
            User user = (User) applicationContext.getBean("User");
            user.setAddress("安徽省合肥市");
            user.setId("1238784");
            user.setName("HeloWxl");
            System.out.println(user.toString());
            user.useCreateMethod("ioc");
        }
    
    ioc.png

    相关文章

      网友评论

          本文标题:Spring-IOC的发展

          本文链接:https://www.haomeiwen.com/subject/abnqnctx.html