Spring - 03 - bean的作用域

Bean的作用域


在默认情况下,Spring应用上下文中所有的bean都是作为单例的形式创建的。

也就是说,不管给定的一个bean被注入到其他bean多少次,每次所注入的都是同一个实例。


Spring定义了多种作用域,可以基于这些作用域创建Bean;

• 单例(singleton):在整个应用中,只创建bean的一个实例。

• 原型(prototype):每次注入或者通过Spring应用上下文获取的时候,都会创建一个新的bean实例。

• 会话(request):在Web应用中,为每个会话创建一个bean实例。

• 请求(session):在Web应用中,为每个请求创建一个bean实例。


单例是默认的作用域,对于易变的类型,这并不合适。

如果选择其它的作用域,要使用@Scope注解,它可以与@Component或@Bean注解一起使用。


上面四种,其中比较常用的是单例、原型两种作用域。

单例作用域:每次请求该Bean都将获得相同的实例。容器负责跟踪Bean实例的状态,负责维护Bean实例的生命周期行为。

原型作用域:程序每次请求该id的bean,Spring都会新建一个bean实例,然后返回给调用者。这种情况下,Spring容器仅仅使用new关键字创建Bean实例,一旦创建完成,容器不再跟踪实例,也不会维护Bean实例的状态。


写个小Demo看下

package com.test.spring.server.test07;

/**
 * @author CYX
 * @create 2018-04-30-21:37
 */
public class HelloWorld {

    private String message;


    public void getMessage() {
        System.out.println("the message : " + message);
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <bean id="helloWorld" class="com.test.spring.server.test07.HelloWorld"></bean>

</beans>
package com.test.spring.server.test07;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author CYX
 * @create 2018-04-30-21:36
 */
public class Test07App {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("spring/test07/applicationContext07.xml");

        HelloWorld helloWorld = context.getBean("helloWorld", HelloWorld.class);
        System.out.println(helloWorld);
        helloWorld.setMessage("123");
        helloWorld.getMessage();

        HelloWorld helloWorld2 = context.getBean("helloWorld", HelloWorld.class);
        helloWorld2.getMessage();
        System.out.println(helloWorld2);
        helloWorld2.setMessage("456");
        helloWorld2.getMessage();

        System.out.println(helloWorld == helloWorld2);

    }

}

输出结果:

在 bean 的配置中,没有设置任何关于作用域的配置,所以它是默认使用单例。

看两次打印的对象地址都是一样。


我们在来看原型作用域

将applicationContext.xml中的配置修改一下,其他代码不变:

<bean id="helloWorld" class="com.test.spring.server.test07.HelloWorld" scope = "prototype"/>

输出结果:


对象两次打印出来的内存地址也是不一样的。

每次向Spring 请求一次,都会重新new一个对象给我....


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值