===================
对象的生命周期_创建
===================
Spring工厂何时创建对象?
- scope="singleton":Spring工厂创建的同时,对象即被创建
- scope="prototype":何时获取(getBean())何时创建
/src/main/java/cn/tedu/injection/Product.java:
public class Product {
private Logger logger = Logger.getLogger(Product.class);
public Product() {
logger.debug("Product.Product");
}
}
/src/main/resources/applicationContext.xml:
Before ApplicationContext
Product.Product <- 建厂时创建对象(饿汉单例)
After ApplicationContext
Before getBean
After getBean
cn.tedu.injection.Product@6bd61f98
/src/main/resources/applicationContext.xml:
Before ApplicationContext
After ApplicationContext
Before getBean
Product.Product <- 获取时创建对象(多例)
After getBean
cn.tedu.injection.Product@6bd61f98
例程:Injection
即使scope="singleton",即单例,也是可以做到“获取时创建对象”的。
/src/main/resources/applicationContext.xml:
Before ApplicationContext
After ApplicationContext
Before getBean
Product.Product <- 获取时创建对象(懒汉单例)
After getBean
cn.tedu.injection.Product@6bd61f98
例程:Injection