标签:请求 portlet etc let 3.1 set app prot 多例
1.<bean>的属性;
2.作用:控制对象有效范围(单例,多例等)
3.<bean/>标签对应的对象默认是单例的.
3.1无论获取多少次,都是同一个对象
Teacher t1 = ac.getBean("teacher1",Teacher.class); System.out.println(t1); Teacher t2 = ac.getBean("teacher1",Teacher.class); System.out.println(t2); System.out.println(t1==t2);
3.3输出结果如下: 3.4com.test.Teacher@5b275dab 3.5com.test.Teacher@5b275dab 3.6true
3.7
4.scope 可取值
4.1singleton 默认值,单例--所有同一对象使用同一地址!
<bean id="teacher1" class="com.test.Teacher" scope="singleton"></bean> 等价于下面的: <bean id="teacher1" class="com.test.Teacher" ></bean>
4.4prototype 多例,每次获取重新实例化该Class !
<bean id="teacher1" class="com.test.Teacher" scope="prototype"></bean> Teacher t1 = ac.getBean("teacher1",Teacher.class); t1.setCnt(2); System.out.println(t1); //Teacher{cnt=2} Teacher t2 = ac.getBean("teacher1",Teacher.class); System.out.println(t2); //Teacher{cnt=2} System.out.println(t1==t2); 输出结果: Teacher{cnt=2} ,Teacher{cnt=0} , false
4.8 request 在每次请求时,重新new() / 实例化;
4.9 session 每个会话对象内,对象是单例的. (依赖于Spring0MVC架构)
4.10 application 在application 对象内是单例;
4.11 global session, spring 推 出 的 一 个 对 象 , 依 赖 于 spring-webmvc-portlet ,类似于session
Spring -10 -<bean>的 scope 属性 -
标签:请求 portlet etc let 3.1 set app prot 多例
原文地址:https://www.cnblogs.com/zhazhaacmer/p/10113353.html