对象的中间有两种,一种是:通过工厂获得IOC容器,然后通过IOC容器创建对象,另一种是直接创建对象
首先我们先创建一个User类
package cn.qblank.hellospring;
public class User {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(String id, String name) {
super();
this.id = id;
this.name = name;
}
public User() {
System.out.println("---创建对象---");
}
public void init_user(){
System.out.println("---初始化对象---");
}
public void destory_user(){
System.out.println("---对象的销毁---");
}
}
然后通过applicationContext.xml对象将对象名和对象的完整类名存入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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="user" class="cn.qblank.hellospring.User"></bean>
</beans>
然后编写对象创建的相关方法
package cn.qblank.hellospring;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
public class Hello {
/**
* 方法一: 通过工厂类得到IOC容器创建的对象
* @throws Exception
*/
@Test
public void testIOC() throws Exception {
//创建User对象
//把对象交给spring的IOC容器
ClassPathResource resource = new ClassPathResource("cn/qblank/hellospring/applicationContext.xml");
//创建容器工厂
BeanFactory factory = new XmlBeanFactory(resource);
//通过工厂得到User对象
User user = (User) factory.getBean("user");
System.out.println(user);
}
/**
* 方法二: (常用)直接得到IOC对象
* @throws Exception
*/
@Test
public void testAc() throws Exception {
//获取IOC对象
ApplicationContext ac = new ClassPathXmlApplicationContext("/cn/qblank/hellospring/applicationContext.xml");
//从容器中通过id获取bean
User user = (User) ac.getBean("user");
//测试
System.out.println(user);
}
}
通过测试我们发现这些对象是同一个对象。
于是我们就是时候探讨一下对象的创建模式了
1) 对象创建: 单例/多例
scope="singleton", 默认值, 即 默认是单例【service/dao/工具类】
scope="prototype", 多例; 【Action对象】
2) 什么时候创建?
scope="prototype" 在用到对象的时候,才创建对象。
scope="singleton" 在启动(容器初始化之前), 就已经创建了bean,且整个应用只有一个。
3)是否延迟创建
azy-init="false" 默认为false, 不延迟创建,即在启动时候就创建对象
lazy-init="true" 延迟初始化, 在用到对象的时候才创建对象
(只对单例有效)
4) 创建对象之后,初始化/销毁
init-method="init_user" 【对应对象的init_user方法,在对象创建爱之后执行 】
destroy-method="destroy_user" 【在调用容器对象的destriy方法时候执行,(容器用实现类)】
这些配置我们可以加载applicationContext.xml中的对应的bean标签内进行配置,我们可以写一个方法对其对象的创建模式进行测试,在这里我们就不一一去尝试了
public void test1() throws Exception {
// 得到IOC容器对象 【用实现类,因为要调用销毁的方法】
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("cn/qblank/hellospring/applicationContext.xml");
System.out.println("---容器创建---");
//测试
User user1 = (User) ac.getBean("user");
User user2 = (User) ac.getBean("user");
System.out.println(user1);
System.out.println(user2);
//销毁容器对象
ac.destroy();
}