Spring--bean的作用范围

在Spring中,bean的作用范围分以下几种: 

  singleton:spring ioc容器中仅有一个bean实例,bean以单例的方式存在
  prototype:每次从容器中调用bean时,都返回一个新的实例
  request:每次http请求都会创建一个新的bean
  session:同一个http session共享一个bean
  global session:同一个全局session共享一个bean 一般用于portlet应用环境
  application:同一个application共享一个bean

singleton

 1 package com.fuwh.test;
 2 
 3 public class Dog {
 4     
 5     public int id;
 6     public String name;
 7     public int age;
 8     public int getId() {
 9         return id;
10     }
11     public void setId(int id) {
12         this.id = id;
13     }
14     public String getName() {
15         return name;
16     }
17     public void setName(String name) {
18         this.name = name;
19     }
20     public int getAge() {
21         return age;
22     }
23     public void setAge(int age) {
24         this.age = age;
25     }
26     @Override
27     public String toString() {
28         return "Dog [id=" + id + ", name=" + name + ", age=" + age + "]";
29     }
30     
31     
32 }
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <bean id="dog" class="com.fuwh.test.Dog" scope="singleton">
 8         <property name="id" value="001"></property>
 9         <property name="name" value="heihu"></property>
10         <property name="age" value="5"></property>
11     </bean>   
12     
13 
14 </beans>
 1 package com.fuwh.test;
 2 
 3 
 4 import org.junit.Before;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 public class Test {
 9 
10     private ApplicationContext ac;    
11 
12     @Before
13     public void setUp() throws Exception {
14         ac=new ClassPathXmlApplicationContext("beans.xml");
15     }
16 
17     @org.junit.Test
18     public void test() {
19         System.out.println(ac.getBean("dog")==ac.getBean("dog"));
20     }
21 }

返回结果 : true

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <bean id="dog" class="com.fuwh.test.Dog" scope="prototype">
 8         <property name="id" value="001"></property>
 9         <property name="name" value="heihu"></property>
10         <property name="age" value="5"></property>
11     </bean>   
12     
13 
14 </beans>
 1 package com.fuwh.test;
 2 
 3 
 4 import org.junit.Before;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 public class Test {
 9 
10     private ApplicationContext ac;    
11 
12     @Before
13     public void setUp() throws Exception {
14         ac=new ClassPathXmlApplicationContext("beans.xml");
15     }
16 
17     @org.junit.Test
18     public void test() {
19         System.out.println(ac.getBean("dog")==ac.getBean("dog"));
20     }
21 }

返回结果:false

 

posted @ 2017-01-10 19:59  Ouka傅  阅读(3509)  评论(0编辑  收藏  举报