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(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
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(){
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);
}
}
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);
}
}