实例化,执行,初始化,销毁bean对象
2014-11-23 22:56
239 查看
1.修改applicationContext.xml,通过设置配置文件属性lazy-init="true",可以改变Spring容器创建对象的时机。
2.在顶级的<beans/>元素中的default-lazy-init属性,可以为容器所有<bean>指定延迟实例化特性
3.从输出结果可以看出,当使用ExampleBean对象时,才被创建,即,设置lazy-init="true"属性后,对象不使用不创建。
package org.spring.dao;
public class ExampleBean {
public ExampleBean() {
System.out.println("实例化ExampleBean");
}
public void execute() {
System.out.println("执行ExampleBean处理");
}
public void init() {
System.out.println("初始化ExampleBean对象");
}
public void destroy() {
System.out.println("销毁ExampleBean对象");
}
}
<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd"
default-lazy-init="true"
default-init-method="init"
default-destroy-method="destroy">
<bean id="exampleBean" class="org.spring.dao.ExampleBean" scope="singleton"
init-method="init" destroy-method="destroy" depends-on="bean1" />
<bean id="bean1" class="org.spring.dao.ExampleBean1" lazy-init="true" />
</beans>
package org.spring.test;
import org.junit.Test;
import org.spring.dao.ExampleBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testCase {
@Test
public void testExampleBean() {
// 实例化Spring容器示例
String conf = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//System.out.println(ac);
// 获取ExampleBean对象
ExampleBean bean1 = ac.getBean("exampleBean", ExampleBean.class);
ExampleBean bean2 = ac.getBean("exampleBean", ExampleBean.class);
System.out.println(bean1 == bean2);
// 关闭Spring容器, 注意AbstractApplicationContext类型定义了 close()方法
AbstractApplicationContext ctx = (AbstractApplicationContext) ac;
ctx.close();
}
}
2.在顶级的<beans/>元素中的default-lazy-init属性,可以为容器所有<bean>指定延迟实例化特性
3.从输出结果可以看出,当使用ExampleBean对象时,才被创建,即,设置lazy-init="true"属性后,对象不使用不创建。
package org.spring.dao;
public class ExampleBean {
public ExampleBean() {
System.out.println("实例化ExampleBean");
}
public void execute() {
System.out.println("执行ExampleBean处理");
}
public void init() {
System.out.println("初始化ExampleBean对象");
}
public void destroy() {
System.out.println("销毁ExampleBean对象");
}
}
package org.spring.dao; public class ExampleBean1 { public ExampleBean1() { System.out.println("实例化ExampleBean1"); } }
<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd"
default-lazy-init="true"
default-init-method="init"
default-destroy-method="destroy">
<bean id="exampleBean" class="org.spring.dao.ExampleBean" scope="singleton"
init-method="init" destroy-method="destroy" depends-on="bean1" />
<bean id="bean1" class="org.spring.dao.ExampleBean1" lazy-init="true" />
</beans>
package org.spring.test;
import org.junit.Test;
import org.spring.dao.ExampleBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testCase {
@Test
public void testExampleBean() {
// 实例化Spring容器示例
String conf = "applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//System.out.println(ac);
// 获取ExampleBean对象
ExampleBean bean1 = ac.getBean("exampleBean", ExampleBean.class);
ExampleBean bean2 = ac.getBean("exampleBean", ExampleBean.class);
System.out.println(bean1 == bean2);
// 关闭Spring容器, 注意AbstractApplicationContext类型定义了 close()方法
AbstractApplicationContext ctx = (AbstractApplicationContext) ac;
ctx.close();
}
}
相关文章推荐
- Spring中初始化bean和销毁bean的时候执行某个方法的详解
- Spring中初始化bean和销毁bean的时候执行某个方法的详解
- Spring中初始化bean和销毁bean的时候执行某个方法的详解
- Spring初始化Bean或销毁Bean前执行操作的方式
- java类的初始化块/执行顺序,实例化对象数据赋值
- Spring中初始化bean和销毁bean的时候执行某个方法的详解
- Spring 实例化bean的初始化方法和销毁方法 init-method destroy-method
- Spring中初始化bean和销毁bean的时候执行某个方法的详解
- java类的初始化块/执行顺序,实例化对象数据赋值
- Spring中初始化bean和销毁bean的时候执行某个方法的详解
- Cocos2d-x初始化对象和销毁对象函数执行顺序
- Spring学习笔记-Bean初始化&销毁
- spring学习笔记2--Bean的初始化(实例化)、作用域
- Spring笔记:定制Bean的初始化和销毁过程
- Spring创建对象初始化bean的时机分为两种形式:
- java中创建对象,初始化对象,实例化对象的解释
- java中对象如何初始化(实例化)
- 对象初始化的执行顺序
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- 【Effective Java】Ch2_创建销毁对象:Item4_通过私有构造函数来强化不可实例化的能力