Spring Scope与Lazy

来源:互联网 发布:nginx 在线人数统计 编辑:程序博客网 时间:2024/06/03 07:14
package com.sanmao.spring.test;import com.sanmao.spring.ioc.HelloWorld;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Created by root on 16-9-26. */public class ScopeTest {        /**         * 在默认情况下,Spring容器产生的对象是单例的         * */        @Test        public void testScope_Default(){            //启动sping容器            ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");            //从spring容器中把对象提取出来            HelloWorld helloWorld=(HelloWorld)context.getBean("helloWorld");            System.out.println(helloWorld);            HelloWorld helloWorld1=(HelloWorld)context.getBean("helloWorld");            System.out.println(helloWorld1);        }    /**     * 如果scope为prototype,则为多实例     * */    @Test    public void testScope_Prototype(){        //启动sping容器        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");        //从spring容器中把对象提取出来        HelloWorld helloWorld2=(HelloWorld)context.getBean("helloWorld");        HelloWorld helloWorld3=(HelloWorld)context.getBean("helloWorld");        System.out.println(helloWorld2);        System.out.println(helloWorld3);    }}
public class ScopeLazy {    /**     * 如果scope为prototype,则lazy-init 将失去作用     * */    @Test    public void testScope_Prototype_Lazy_Defalut(){        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");        HelloWorld helloWorld2=(HelloWorld)context.getBean("helloWorld");        HelloWorld helloWorld3=(HelloWorld)context.getBean("helloWorld");        System.out.println(helloWorld2);        System.out.println(helloWorld3);    }}
0 0