从头认识Spring-1.6 Bean的创建与销毁操作

Posted 李灵晖

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从头认识Spring-1.6 Bean的创建与销毁操作相关的知识,希望对你有一定的参考价值。

这一章节我们来介绍一下Bean的创建与销毁操作。

在xml的配置里面,我们可以配置Bean的创建与销毁操作。

(1)domain

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_6;

public class Chief {
	private final int id = index++;

	public int getId() {
		return id;
	}

	public void create() {
		System.out.println("chief created");
	}

	public void destroy() {
		System.out.println("chief destroy");
	}

	private static int index = 0;

}

除了定义每一个对象的id之外,还定义了创建和销毁操作的相应的方法create和destroy

(2)配置xml里面的Bean时,我们需要配置init-method="create" destroy-method="destroy"

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
	<bean id="chief"
		class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_6.Chief"
		init-method="create" destroy-method="destroy" />
</beans>

(3)测试类,只是简单的geiId

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_6;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
		"/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_6/ApplicationContext-test.xml" })
public class ChiefTest {

	@Autowired
	private ApplicationContext applicationContext;

	@Test
	public void testChief() {
		Chief chief = applicationContext.getBean(Chief.class);
		System.out.println("chief id:" + chief.getId());
	}
}

输出:

chief created
chief id:0
chief destroy


创建操作发生在对象的创建之后,销毁操作发生在对象的销毁之后。


(4)我们修改一下测试类和domain,让上面的结果更加突出

domain:

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_6;

public class Chief {
	static {
		System.out.println("before chief create");
	}

	public Chief() {
		System.out.println("chief construtor");
	}

	private final int id = index++;

	public int getId() {
		return id;
	}

	public void create() {
		System.out.println("chief created");
	}

	public void destroy() {
		System.out.println("chief destroy");
	}

	private static int index = 0;

}


我们在domain里面增加了优先执行的静态代码块,以及在构造器里面增加了输出


测试类:

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_6;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
		"/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_6/ApplicationContext-test.xml" })
public class ChiefTest {

	@Autowired
	private ApplicationContext applicationContext;

	@Test
	public void testChief() {
		for (int i = 0; i < 3; i++) {
			Chief chief = applicationContext.getBean(Chief.class);
			System.out.println("chief id:" + chief.getId());
		}
	}
}

在测试类里面我们增加了循环取id的操作。


测试输出:

before chief create
chief construtor
chief created
chief id:0
chief id:0
chief id:0
chief destroy


有输出可以看出,执行顺序是:

(1)静态代码块

(2)构造函数,创建对象

(3)创建操作

(4)执行业务操作

(5)销毁操作


总结:这一章节简单介绍了Bean的创建与销毁操作。


目录:http://blog.csdn.net/raylee2007/article/details/50611627 

 

我的github:https://github.com/raylee2015/my_new_spring