zoukankan      html  css  js  c++  java
  • spring中的bean的属性scope

    spring中bean的scope属性,有如下5种类型:

    1. singleton 表示在spring容器中的单例,通过spring容器获得该bean时总是返回唯一的实例
    2. prototype表示每次获得bean都会生成一个新的对象
    3. request表示在一次http请求内有效(只适用于web应用)
    4. session表示在一个用户会话内有效(只适用于web应用)
    5. globalSession表示在全局会话内有效(只适用于web应用)

    在多数情况,我们只会使用singleton和prototype两种scope,如果在spring配置文件内未指定scope属性,默认为singleton。

    下面我们用一个示例来说明singleton和prototype两种scope的区别。

    添加java类Person,Person的内容如下:

    package cn.outofmemory.spring;
    
    public class Person {
        private int id;
        private String name;
        
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }

    Person类是一个POJO类,我们不需要他做任何事情,只是为了证明prototype和singleton两种scope的异同之处。

    我们还需要一个App类,他的代码如下:

    package cn.outofmemory.spring;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Hello world!
     *
     */
    public class App 
    {
        public static void main( String[] args )
        {
            ApplicationContext appContext = new ClassPathXmlApplicationContext("/spring.xml");
            Person p1 = appContext.getBean(Person.class);
            System.out.println("p1's identityHashCode is " + System.identityHashCode(p1));
            
            Person p2 = appContext.getBean(Person.class);
            System.out.println("p2's identityHashCode is " + System.identityHashCode(p2));
            
        }
    }

    在App类中的main方法中,首先我们初始化了ApplicationContext的实例,然后分别获得两次Person bean的实例p1,p2并分别输出其identityHashCode,如果两个对象是同一个对象,那么他们的identityHashCode应该是一致的。

    添加source folder:src/main/conf,并新建spring配置文件spring.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd">    
        <bean class="cn.outofmemory.spring.Person">
            <property name="id" value="1"/>
            <property name="name" value="Jim"/>
        </bean>    
    </beans>

    在此配置文件中我们定义了Person bean,没有指定其scope,这时候bean的scope为默认的singleton,也就是单例,此时App类的输出应该是两个相同的identityHashcode,我们运行程序看输出的结果:

    p1's identityHashCode is 23954271
    p2's identityHashCode is 23954271


        <bean class="cn.outofmemory.spring.Person" scope="prototype">
            <property name="id" value="1"/>
            <property name="name" value="Jim"/>
        </bean>    

    再次运行程序,输出结果如下:

    p1's identityHashCode is 23954271
    p2's identityHashCode is 13359324

    可以看到p1和p2是两个不同的值,这说明scope是prototype的情况下,同一个bean定义会返回不同的对象。

    我们也可以通过Scope注解来指定java bean的scope,我们给Person类添加如下注解:

    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
    
    @Component
    @Scope("prototype")
    public class Person {
    ....省略
    }

    @Component注解告诉spring,要加载此类,Scope注解bean的scope是prototype。

    我们修改spring.xml配置文件,使用context:component-scan节点,让spring通过注解加载bean。

    <?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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd">	
    	 <context:component-scan base-package="cn.outofmemory.spring"></context:component-scan>	
    </beans>

    再次运行程序,将输出:

    p1's identityHashCode is 33212498
    p2's identityHashCode is 24480977

     可以看到使用Scope注解指定prototype scope后,两个Person对象是两个不同的对象。 

    原文地址 : http://outofmemory.cn/java/spring/spring-bean-scope

  • 相关阅读:
    几行代码搞定图片模糊模式
    SVN源代码管理规范
    模仿苹果手机虚拟键的代码分享,有兴趣的可以玩玩。 下面的是链接,复制粘贴到浏览器就能下载
    keytool使用方法
    Unity导出APk出错解决方法二
    学习网址
    eclipse中手动设置library,选择编译工具方法
    apk接入google play邮箱登陆及充值注意事项
    unity导出apk错误出错解决方法
    java自动生成jar包工具
  • 原文地址:https://www.cnblogs.com/wgbs25673578/p/5617700.html
Copyright © 2011-2022 走看看