您的位置:首页 > 其它

实例化,执行,初始化,销毁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对象");
}

}

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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: