Bean的常用配置,spring单例和多例的用法。

  • id 和 name
    – id和name实际上配置的是bean的别名。
    – id属性在IOC容器中必须唯一,而name一般来说也应该是唯一的。
    – name的属性值可以包含特殊字符,id的属性值则不可以。除此外id和name基本是一样的。

  • class
    – 用户设置一个类的完全路径,用于IOC容器生成类的实例。

  • scope
    – Spring创建对象是单例还是多例取决于Bean的作用域。
    Bean的作用域

类别说明
singleton (默认情况)在SpringIOC容器中仅存在一个Bean实例,Bean以单例的方式存在
prototype每次调用getBean()时都会返回一个新的实例
request每次HTTP请求都会创建一个新的Bean,作用域仅适用于WebApplicationConntext
session同一个HTTP Session共享一个Bean,不同的HTTP Session 使用不同的Bean。改作用域仅适用于WebApplicationContext环境

Person类

package demo4;

public class Person {
    Person(){
        System.out.println("Person");
    }
}

TestPerson类

package demo4;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestPerson {
    @Test
    public void test1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person p1 = (Person) applicationContext.getBean("person");
        Person p2 = (Person) applicationContext.getBean("person");

        System.out.println(p1.hashCode());
        System.out.println(p2.hashCode());
    }
}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="person" class="demo4.Person"></bean>
</beans>

输出结果:单例
在这里插入图片描述
applicationContext.xml配置文件:scope="singleton"

<?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.xsd">
    <bean id="person" class="demo4.Person" scope="singleton"></bean>
</beans>

输出结果:单例
在这里插入图片描述
applicationContext.xml配置文件:scope="prototype"

<?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.xsd">
    <bean id="person" class="demo4.Person" scope="prototype"></bean>
</beans>

输出结果:多例
在这里插入图片描述
综上所述:Bean的作用域范围默认和singleton的情况下返回的是单例,prototype返回的是多例。
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jaywei.online

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值