zoukankan      html  css  js  c++  java
  • Spring4 In Action-3.2@Scope单例、多例Bean

    Spring In Action-3.2@Scope单例、多例Bean

    代码下载地址:http://download.csdn.net/download/poiuy1991719/9967494

    Singleton:单例    
    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
    @Scope("singleton")

    Prototype:每次注入都会创建新的  
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    @Scope("prototype") 

    Session:Web的每一次会话    

     Request:Web的每一次请求

    package com.bean.conditional;
    
    import org.springframework.beans.factory.config.ConfigurableBeanFactory;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
    
    @Component("magicBeanName")//重命名:配置类里面的方法与之对应
    public class MagicBean {
        private String name="MagicBean";
        public MagicBean() {
            System.out.println("MagicBean:实例化"+this);
        }
        
        public void showName(){
            System.out.println("Show Name is:"+name);
        }
    }
    package com.bean.conditional;
    
    import org.springframework.beans.factory.config.ConfigurableBeanFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Scope;
    
    @Configuration
    @ComponentScan(basePackages={"com.bean"})
    public class BeanConfigs {
    
        @Bean
        //Singleton:单例  Prototype:每次注入都会创建新的 Session:Web的每一次会话  Request:Web的每一次请求
    //    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
        @Scope("prototype")
        //@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
    //    @Scope("singleton")
        public MagicBean magicBeanName(){//方法名来源,Bean类的注解:@Component("magicBeanName")
            return new MagicBean();
        }
    }
    package test;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import com.bean.conditional.BeanConfigs;
    import com.bean.conditional.MagicBean;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes={BeanConfigs.class})
    public class test {
        @Autowired
        MagicBean mb;
        
        @Autowired
        MagicBean mb1;
    
        @Test
        public void selfTest(){
            mb.showName();
            mb1.showName();
        }
    }

    也可以通过xml文件配置来实现:

    <bean id="magicBean" scope="prototype"></bean>
  • 相关阅读:
    负载均衡器部署方式和工作原理
    Android 有关于* daemon not running.starting it now on port 5037 *ADB
    微信开发常用文档及参考资料
    XML解析之sax解析案例(二)使用sax解析把 xml文档封装成对象
    XML解析之sax解析案例(一)读取contact.xml文件,完整输出文档内容
    XML解析之SAX解析过程代码详解
    通过PHP current()函数获取未知字符键名数组第一个元素的值
    PHP检测链接是否是SSL连接 ,也就是判断HTTPS
    PHP反射ReflectionClass、ReflectionMethod 入门教程
    PHP 反射API说明
  • 原文地址:https://www.cnblogs.com/zjsy/p/7487529.html
Copyright © 2011-2022 走看看