• Spring核心技术之Bean的作用域 Mr


    在spring中bean的作用域是spring容器用来返回调用者实例类型的

    在spring中有5中类型的作用域:

    先来比较一下singeton 和 prototype:

    package springapp.test;
    
      
    /**     
     * @author zhangxuegang       
     * @version 1.0     
     * @created 2012-10-16 下午11:09:38    
     */
    
    public class CustomerService 
    {
    	String message;
     
    	public String getMessage() {
    		return message;
    	}
     
    	public void setMessage(String message) {
    		this.message = message;
    	}
    }
    

    Singeton的例子当不指定作用域的时候spring默认的为singeton

    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
               http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
               http://www.springframework.org/schema/aop 
               http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
               http://www.springframework.org/schema/tx 
               http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    	<bean id="testSpring" class="springapp.test.TestSpring">
    	</bean>
    	<bean id="exampleBean" class="springapp.test.ExampleBean"
    		factory-method="createInstance">
    		<constructor-arg type="int" value="7500000" />
    		<constructor-arg type="java.lang.String" value="42" />
    	</bean>
    	<bean id="customerService" class="springapp.test.CustomerService" />
    </beans>
    

      

    测试:

    package springapp.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
      
    /**     
     *      
     * 测试作用域为singeton    
     * @author zhangxuegang       
     * @version 1.0     
     * @created 2012-10-16 下午11:12:55    
     */
    
    public class TestSingeton {
        public static void main( String[] args )
        {
            ApplicationContext ctx = new ClassPathXmlApplicationContext(  
            "file:F:/springProject/springapp/war/WEB-INF/applicationTest.xml");
     
        	CustomerService custA = (CustomerService)ctx.getBean("customerService");
        	custA.setMessage("Message by custA");
        	System.out.println("Message : " + custA.getMessage());
     
        	//retrieve it again
        	CustomerService custB = (CustomerService)ctx.getBean("customerService");
        	System.out.println("Message : " + custB.getMessage());
        }
    }
    

      

    运行结果:

    Message : Message by custA
    Message : Message by custA
    

    我们可以看到当作用为singleton时,无论你调用多少次getbean方法,

    返回的都是一样的实例,因为在spring容器中只产生了一个单例的实例

     

    当你每次调用customerServicebean的时候都想返回一个新的实例,那么你需要用prototype去替换它:

    <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
    

    基于注解的bean作用域:

    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>
    

      

     

      

    Mr-sniper
    北京市海淀区
    邮箱:rafx_z@hotmail.com
  • 相关阅读:
    gcc和g++的区别
    configure svn server on win
    FD_SET,FD_ISSET,FD_ZERO,select
    intel中的cr寄存器
    Linux系统环境下的Socket编程详细解析
    可重入函数与不可重入函数
    初步认识迭代服务器和并发服务器
    排序
    fd_set 用法
    MFC消息映射
  • 原文地址:https://www.cnblogs.com/rafx/p/scopeofebean.html
Copyright © 2020-2023  润新知