• Spring中bean的范围


    Spring中有5种bean的范围:

    5 types of bean scopes supported :

    1. singleton – Return a single bean instance per Spring IoC container 这个范围也是默认的
    2. prototype – Return a new bean instance each time when requested
    3. request – Return a single bean instance per HTTP request. *
    4. session – Return a single bean instance per HTTP session. *
    5. globalSession – Return a single bean instance per global HTTP session. *

    P.S * means only valid in the context of a web-aware Spring ApplicationContext

    我们看个关于singleton and prototype.的例子:

    public class CustomerService 
    {
    	String message;
     
    	public String getMessage() {
    		return message;
    	}
     
    	public void setMessage(String message) {
    		this.message = message;
    	}
    }

    1. Singleton example

    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     
           <bean id="customerService" 
                class="com.mkyong.customer.services.CustomerService" />
     
    </beans>
    

      运行:

    public class App 
    {
        public static void main( String[] args )
        {
        	ApplicationContext context = 
        	 new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
     
        	CustomerService custA = (CustomerService)context.getBean("customerService");
        	custA.setMessage("Message by custA");
        	System.out.println("Message : " + custA.getMessage());
     
        	//retrieve it again
        	CustomerService custB = (CustomerService)context.getBean("customerService");
        	System.out.println("Message : " + custB.getMessage());
        }
    }
    

      输出为:

    Message : Message by custA
    Message : Message by custA

    2. Prototype example

    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     
       <bean id="customerService" class="com.mkyong.customer.services.CustomerService" 
             scope="prototype"/>
     
    </beans>
    

      运行输出为:

    Message : Message by custA
    Message : null

    也可以使用注解来做这些事情:
    package com.mkyong.customer.services;
     
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Service;
     
    @Service
    @Scope("prototype")
    public class CustomerService 
    {
    	String message;
     
    	public String getMessage() {
    		return message;
    	}
     
    	public void setMessage(String message) {
    		this.message = message;
    	}
    }
    

      

    <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-2.5.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context-2.5.xsd">
     
           <context:component-scan base-package="com.mkyong.customer" />
     
    </beans>
    

      


    ==============================================================================

    本博客已经废弃,不在维护。新博客地址:http://wenchao.ren


    我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他
    们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其
    实我是一个程序员

    ==============================================================================
  • 相关阅读:
    几种基本样式,背景图,字体,下划线,行高垂直等
    网页主菜单,横向
    DOM操作
    递归的小例题
    学习两个星期后做的第一个网页
    Js的语法和循环
    JS
    75 int类型数组中除了一个数出现一次或两次以外,其他数都出现三次,求这个数。[2行核心代码]
    74 使用BitSet输出数组中的重复元素
    73 [面试题]交换一个整数的二进制表示的奇偶位(swapOddEvenBits)
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2835109.html
Copyright © 2020-2023  润新知