美文网首页
spring容器创建对象原理

spring容器创建对象原理

作者: simplerandom | 来源:发表于2020-07-23 10:11 被阅读0次

多例对象

public class Test {
    public static void main(String[] args)
            throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        // 加载类路径下配置文件
        InputStream resourceAsStream = Test.class.getClassLoader().getResourceAsStream("class.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        Object hello = properties.get("hello");
        System.out.println(hello);
        // 根据全限定类目创建对象
        Class<Hello> aClass = (Class<Hello>) Class.forName((String) hello);
        System.out.println(aClass.newInstance().toString());
    }
}

单例对象

public class Test {
static  HashMap<String, Object> map = new HashMap<>();
    public static void main(String[] args)
            throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        // 加载类路径下配置文件
        InputStream resourceAsStream = Test.class.getClassLoader().getResourceAsStream("class.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        Object hello = properties.get("hello");
        System.out.println(hello);
        // 根据全限定类目创建对象
        Class<Hello> aClass = (Class<Hello>) Class.forName((String) hello);
        Hello instance = aClass.newInstance();
        map.put("hello",instance);
    }
}

相关文章

  • 注解

    依赖注入的注解的原理: 1、当启动spring容器的时候,创建两个对象2、当spring容器解析到 spring容...

  • Spring 学习笔记(二):Spring 容器以及 bean

    Spring 容器 在 Spring 应用中,应用对象生存于 Spring 容器中,Spring 容器负责创建对象...

  • spring容器创建对象原理

    多例对象 单例对象

  • Hibernate,Struts2,Spring整合

    整合原理 Struct2与Spring整合:将Action对象交与Spring容器负责创建 Hibernate与S...

  • spring 学习02

    Spring 容器 在Spring 应用中,你的应用对象生存在Spring容器中,Spring 容器负责创建对象,...

  • 2.Spring IoC 容器

    1.Spring IoC 容器 IoC 容器 Spring 容器是 Spring 框架的核心。容器将创建对象,把它...

  • Spring 讲解(二 )

    1、Spring 容器加载的3种方式 Spring内部创建对象的原理 a.解析xml文件,获取类名,id,属性等。...

  • Spring知识清单

    1什么是Spring Bean容器? 用于创建bean对象,管理bean对象的那个容器。 2Spring Bean...

  • Spring Ioc DI

    Spring Ioc 容器是Spring框架的核心。只需要进行简单的容器配置,就能将创建对象,使用对象,销毁对象联...

  • spring_IOC总结(二)--xml依赖注入

    spring的bean对象--依赖注入 spring 创建bean对象细节 配置spring核心容器xml配置文件...

网友评论

      本文标题:spring容器创建对象原理

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