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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM