Spring作用域相关知识singleton 和 prototype

创建一个user对象

@Data
public class User {
    private String id;
    private String name;
    private Integer age;
}

创建一个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"
       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">

    <!--    singleton-->
    <bean id="UserSingleton" class="com.chenxi.demo.po.User" scope="singleton">
        <property name="id" value="1"/>
        <property name="name" value="辰兮"/>
        <property name="age" value="22"/>
    </bean>

    <!--    prototype-->
    <bean id="UserPrototype" class="com.chenxi.demo.po.User" scope="prototype" >
        <property name="id" value="2"/>
        <property name="name" value="辰兮要努力"/>
        <property name="age" value="23"/>
    </bean>

</beans>

创建一个测试类帮助我们学习ApplicationContext

import com.chenxi.demo.po.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @program: ApplicationContextDemo
 * @description: ApplicationContext入门学习
 * @author: 辰兮要努力
 * @create: 2021-11-10 22:07:54
 */
public class ApplicationContextDemo {
    //打印日志
    private static final Logger logger = LoggerFactory.getLogger(ApplicationContextDemo.class);
    public static void main(String[] args) {
        /**
         *
         * ApplicationContext体系结构
         * 主要实现类:
         * ClassPathXmlApplicationContext:默认从类路径加载配置文件
         * FileSystemXmlApplicationContext:默认从文件系统中装载配置文件
         */
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        /**
         * Singleton:单实例(在容器启动完成之前就已经创建好了,保存在容器中了,任何时候获取都是获取之前创建好的那个对象)
         */
        User user = (User) applicationContext.getBean("UserSingleton");
        User user1 = (User) applicationContext.getBean("UserSingleton");
        /**
         * 换一种写法:<T> T getBean(String var1, Class<T> var2) throws BeansException;
         */
        User user2 =  applicationContext.getBean("UserSingleton",User.class);
        logger.info("user.hashCode()是:{}",user.hashCode());
        logger.info("user1.hashCode()是:{}",user1.hashCode());
        logger.info("user是:{}",user);
        logger.info("user1是:{}",user1);
        logger.info("user == user1 :{}",user == user1);
        /**
         * Prototype:多实例(容器启动默认不会创建多实例bean对象,只有在获取的时候才创建,每次获取都会创建一个新的实例对象)
         */
        User user3 = (User) applicationContext.getBean("UserPrototype");
        User user4 = (User) applicationContext.getBean("UserPrototype");
        logger.info("user3.hashCode()是:{}",user3.hashCode());
        logger.info("user4.hashCode()是:{}",user4.hashCode());
        logger.info("user3是:{}",user3);
        logger.info("user4是:{}",user4);
        logger.info("user3 == user4 :{}",user3 == user4);

    }
}

执行代码:控制台日志

22:00:32.921 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user.hashCode():1446002
22:00:32.922 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user1.hashCode():1446002
22:00:32.922 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user是:User(id=1, name=辰兮, age=22)
22:00:32.922 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user1是:User(id=1, name=辰兮, age=22)
22:00:32.922 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user == user1 :true
22:00:32.923 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user3.hashCode():266875004
22:00:32.923 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user4.hashCode():266875004
22:00:32.923 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user3是:User(id=2, name=辰兮要努力, age=23)
22:00:32.923 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user4是:User(id=2, name=辰兮要努力, age=23)
22:00:32.923 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user3 == user4 :false
 

Singleton:单实例(在容器启动完成之前就已经创建好了,保存在容器中了,任何时候获取都是获取之前创建好的那个对象)

Prototype:多实例(容器启动默认不会创建多实例bean对象,只有在获取的时候才创建,每次获取都会创建一个新的实例对象)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值