zoukankan      html  css  js  c++  java
  • Spring入门第八课

    看如下代码

    <?xml version="1.0" encoding="UTF-8"?>
    <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.xsd">
        
        <bean id="car" class="logan.spring.study.autowire.Car">
            <property name="brand" value="Audi"></property>
            <property name="price" value="3000000"></property>
        </bean>
    </beans>
    package logan.spring.study.scope;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import logan.spring.study.autowire.Car;
    
    public class Main {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
            
            Car car1 = (Car) ctx.getBean("car");
            Car car2 = (Car) ctx.getBean("car");
            
            System.out.println(car1 == car2);
            
            
    
        }
    
    }

    输出结果为:

    五月 20, 2017 4:39:17 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Sat May 20 16:39:17 CST 2017]; root of context hierarchy
    五月 20, 2017 4:39:17 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [beans-scope.xml]
    true

    可以看到从IOT获得的Car实例是单例的。

    事实上,我们可以设置bean的作用域,如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <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.xsd">
        
        <bean id="car" class="logan.spring.study.autowire.Car" scope="prototype">
            <property name="brand" value="Audi"></property>
            <property name="price" value="3000000"></property>
        </bean>
    </beans>
    package logan.spring.study.scope;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import logan.spring.study.autowire.Car;
    
    public class Main {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
            
            Car car1 = (Car) ctx.getBean("car");
            Car car2 = (Car) ctx.getBean("car");
            
            System.out.println(car1 == car2);
        }
    }

    下面是输出结果

    五月 20, 2017 4:42:00 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Sat May 20 16:42:00 CST 2017]; root of context hierarchy
    五月 20, 2017 4:42:00 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [beans-scope.xml]
    false

    使用prototype作用域,bean的实例就不是单例的了。每次获取Bean的时候返回的都是新的Bean。默认值是单例的。

  • 相关阅读:
    redis_03 _ 高性能IO模型:为什么单线程Redis能那么快
    redis_02 _ 数据结构:快速的Redis有哪些慢操作?
    redis-01 _ 基本架构:一个键值数据库包含什么?
    mysql_28 _ 读写分离有哪些坑
    mysql_27 _ 主库出问题了,从库怎么办
    小程序的转发功能
    简单几何(求交点) UVA 11178 Morley's Theorem
    测试开发CICD——Docker——windows8上环境安装
    测试开发进阶——spring boot——MVC——MyBatis初步了解(转载)
    BZOJ 2226 [Spoj 5971] LCMSum
  • 原文地址:https://www.cnblogs.com/LoganChen/p/6882506.html
Copyright © 2011-2022 走看看