zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    2016改变与突破的一年
    web安全测试&渗透测试之sql注入~~
    后台服务端接口验证项目实例~~
    Web自动化框架之五一套完整demo的点点滴滴(excel功能案例参数化+业务功能分层设计+mysql数据存储封装+截图+日志+测试报告+对接缺陷管理系统+自动编译部署环境+自动验证false、error案例)
    web自动化框架之四测试报告的搭建
    web自动化框架之三获取数据库值与界面值比较~~
    Web自动化框架搭建之二基于数据驱动应用简单实例~~
    web自动化框架之一介绍与环境搭建(Selenium+Eclipse+Python)
    事件响应
    威胁分析
  • 原文地址:https://www.cnblogs.com/zerotomax/p/6270629.html
Copyright © 2011-2022 走看看