zoukankan      html  css  js  c++  java
  • Spring学习(6)---Bean定义及作用域的例子

    (一)Bean的定义

    先定义一个BeanAnnotation

    package com.mypackage;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class BeanAnnotation {
    
    	public void say(String args){
    		System.out.println("BeanAnnotation:"+args);
    	}
    }
    

     XML配置:

    <?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:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-4.1.xsd">
            
            <context:component-scan base-package="com.mypackage">
            </context:component-scan>    
    </beans>
    

     测试:

    package com.mypackage;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    
    public class UnitTest {
    
    	@Test
    	public void test(){
    		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-beansnnotation.xml");  
    		BeanAnnotation bean=(BeanAnnotation)context.getBean("beanAnnotation");
    		bean.say("测试");
    	}
    }
    

     测试结果:

    2015-7-6 11:36:44 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1342a80d: startup date [Mon Jul 06 11:36:44 CST 2015]; root of context hierarchy
    2015-7-6 11:36:44 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [spring-beansnnotation.xml]
    BeanAnnotation:测试

    (二)作用域实例

     指定作用域:

    package com.mypackage;
    
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
    
    @Scope("prototype")
    @Component
    public class BeanAnnotation {
    
    	public void say(String args){
    		System.out.println("BeanAnnotation:"+args);
    	}
    	
    	public void myhashcode(){
    		System.out.println("BeanAnnotation:"+this.hashCode());
    	}
    }
    

     配置XML:

    <?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:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-4.1.xsd">
            
            <context:component-scan base-package="com.mypackage">
            </context:component-scan>    
    </beans>
    

     单元测试:

    package com.mypackage;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    
    public class UnitTest {
    
    	@Test
    	public void test(){
    		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-beansnnotation.xml");  
    		BeanAnnotation bean=(BeanAnnotation)context.getBean("beanAnnotation");
    		bean.myhashcode();
    		
    		BeanAnnotation bean11=(BeanAnnotation)context.getBean("beanAnnotation");
    		bean11.myhashcode();
    		
    	}
    }
    

     结果:

    2015-7-6 12:54:03 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1342a80d: startup date [Mon Jul 06 12:54:03 CST 2015]; root of context hierarchy
    2015-7-6 12:54:03 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [spring-beansnnotation.xml]
    BeanAnnotation:1617829356
    BeanAnnotation:1567531625

    bean的hashcode不一样说明是两个不同的对象。

    把上述的@Scope("prototype")注解,该成@Scope,即使用默认值

    得到的结果:

    2015-7-6 13:00:30 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1342a80d: startup date [Mon Jul 06 13:00:30 CST 2015]; root of context hierarchy
    2015-7-6 13:00:30 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [spring-beansnnotation.xml]
    BeanAnnotation:1617829356
    BeanAnnotation:1617829356

    说明得到的是同一个对象。

  • 相关阅读:
    串口通信中接收数据时延迟处理与缓存处理的解决方案(C#)
    串口通讯接收数据的处理
    在C#程序设计中使用Win32类库
    C# Mutex对象学习经验
    我眼中的C# 3.0 Written by Allen Lee
    利用C#鼠标拖动TreeView节点
    richtextbox内文字自动滚动的例子
    在十六进制字符串与数值类型之间转换 C# 编程指南
    如何:指定符号位置和加载行为
    杂记20110321
  • 原文地址:https://www.cnblogs.com/JsonShare/p/4624129.html
Copyright © 2011-2022 走看看