美文网首页
spring创建的对象

spring创建的对象

作者: 风雪_夜归人 | 来源:发表于2023-11-04 16:49 被阅读0次

    既然Spring能帮助我们创建对象,那么它给我们创建的对象是单例还是非单例呢?

    实操

    1.Application.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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean name="student" class="domain.Student"></bean>
        <bean name="student2" class="domain.Student"></bean>
    </beans>
    

    2.代码如下

            //读取配置文件的方式,传多个文件,使用逗号分割
            BeanFactory beanFactory  = new ClassPathXmlApplicationContext("ApplicationContext.xml","ApplicationContext_Controller.xml");
            Student s1 = (Student) beanFactory.getBean("student");
            Student s2 = (Student) beanFactory.getBean("student");
            System.out.println(s1);
            System.out.println(s2);
            Student s2_1 = (Student) beanFactory.getBean("student2");
            Student s2_2 = (Student) beanFactory.getBean("student2");
            System.out.println(s2_1);
            System.out.println(s2_2);
    

    运行结果

    domain.Student@5cb9f472
    domain.Student@5cb9f472
    domain.Student@cb644e
    domain.Student@cb644e
    

    我们可以看出spirng帮我们创建的对象默认是单例的,但是spring功能很强大,默认是单例,也就是表明可以通过设置改变创建的对象非单例。下面看下如何配置

    
    <?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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean name="student" class="domain.Student" scope="singleton"></bean>
        <bean name="student2" class="domain.Student" scope="prototype"></bean>
    
    </beans>
    

    代码不做变动,直接运行

            //读取配置文件的方式,传多个文件,使用逗号分割
            BeanFactory beanFactory  = new ClassPathXmlApplicationContext("ApplicationContext.xml","ApplicationContext_Controller.xml");
            Student s1 = (Student) beanFactory.getBean("student");
            Student s2 = (Student) beanFactory.getBean("student");
            System.out.println(s1);
            System.out.println(s2);
            Student s2_1 = (Student) beanFactory.getBean("student2");
            Student s2_2 = (Student) beanFactory.getBean("student2");
            System.out.println(s2_1);
            System.out.println(s2_2);
    

    运行结果

    domain.Student@5cb9f472
    domain.Student@5cb9f472
    domain.Student@cb644e
    domain.Student@13805618
    

    总结

    我们可以看出,spirng默认创建的对象是单例的,但是通过在配置文件中设置scope属性,singleton表示单例(不填写默认也是单例);prototype表示非单例。

    那么我们还有个问题,spring创建的对象是立即加载的,还是延迟加载呢?

    1.配置文件不动

    <?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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean name="student" class="domain.Student" scope="singleton"></bean>
    </beans>
    

    2.代码

            //读取配置文件的方式,传多个文件,使用逗号分割
            BeanFactory beanFactory  = new ClassPathXmlApplicationContext("ApplicationContext.xml","ApplicationContext_Controller.xml");
    

    实体类加点标记

        public Student(){
            System.out.println("我是Student的构造方法");
    
        }
    

    运行结果

    我是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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean name="student" class="domain.Student" scope="prototype"></bean>
    </beans>
    

    运行结果

    
    

    修改代码

            //读取配置文件的方式,传多个文件,使用逗号分割
            BeanFactory beanFactory  = new ClassPathXmlApplicationContext("ApplicationContext.xml","ApplicationContext_Controller.xml");
    /*        Student s1 = (Student) beanFactory.getBean("student");
            Student s2 = (Student) beanFactory.getBean("student");
            System.out.println(s1);
            System.out.println(s2);*/
            Student s2_1 = (Student) beanFactory.getBean("student2");
            Student s2_2 = (Student) beanFactory.getBean("student2");
            System.out.println(s2_1);
            System.out.println(s2_2);
    

    总结

    我们可以看出,spirng默认是单例,单例是立即加载的。不去getBean spring也帮你创建好对象。非单例不是立即加载,需要主动getBean才能创建对象。

    但是呢 这样还会有问题,单例会立即创建对象,但是我们使用spring的过程中,对象大部分是需要单例的,且随着业务的复杂程度上去,我们是不希望开始加载spring的同时,就创建这么多对象。也希望有着prototype的效果。随用随创。OK那就继续看下面内容

    1.修改配置

    <?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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean name="student" class="domain.Student" scope="singleton" lazy-init="true"></bean>
    </beans>
    

    2.代码如下

            //读取配置文件的方式,传多个文件,使用逗号分割
            BeanFactory beanFactory  = new ClassPathXmlApplicationContext("ApplicationContext.xml","ApplicationContext_Controller.xml");
    

    3.运行结果

    4.修改代码

            //读取配置文件的方式,传多个文件,使用逗号分割
            BeanFactory beanFactory  = new ClassPathXmlApplicationContext("ApplicationContext.xml","ApplicationContext_Controller.xml");
            Student s1 = (Student) beanFactory.getBean("student");
            Student s2 = (Student) beanFactory.getBean("student");
            System.out.println(s1);
            System.out.println(s2);
    

    5.运行结果

    我是Student的构造方法
    domain.Student@5cb9f472
    domain.Student@5cb9f472
    

    总结

    配置文件中 lazy-init="true" 表示延迟加载。true表示延迟加载,!true的都是立即加载

    相关文章

      网友评论

          本文标题:spring创建的对象

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