spring中bean生命週期學習------java web project中

    1. SpringBean.java--------spring bean
package com.lxh.springBeanLifeCycle;

import javax.servlet.ServletContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.context.ServletContextAware;

public class SpringBean implements BeanNameAware, BeanClassLoaderAware,
		BeanFactoryAware, ResourceLoaderAware, ApplicationEventPublisherAware,
		MessageSourceAware, ApplicationContextAware, ServletContextAware,
		BeanPostProcessor, InitializingBean {
	// 屬性
	private String name;

	// set,get
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	// spring bean 生命週期
	public void setBeanName(String arg0) {
		System.out.println("***BeanNameAware's setBeanName***\n" + arg0);
	}

	public void setBeanClassLoader(ClassLoader arg0) {
		System.out.println("***BeanClassLoaderAware's setBeanClassLoader***\n"
				+ arg0);
	}

	public void setBeanFactory(BeanFactory arg0) throws BeansException {
		System.out.println("***BeanFactoryAware's setBeanFactory***\n" + arg0);
	}

	public void setResourceLoader(ResourceLoader arg0) {
		System.out
				.println("***ResourceLoaderAware's setResourceLoader (only applicable when running in an application context)***\n"
						+ arg0);
	}

	public void setApplicationEventPublisher(ApplicationEventPublisher arg0) {
		System.out
				.println("***ApplicationEventPublisherAware's setApplicationEventPublisher (only applicable when running in an application context)***\n"
						+ arg0);
	}

	public void setMessageSource(MessageSource arg0) {
		System.out
				.println("***MessageSourceAware's setMessageSource (only applicable when running in an application context)***\n"
						+ arg0);
	}

	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		System.out
				.println("***ApplicationContextAware's setApplicationContext (only applicable when running in an application context)***\n"
						+ arg0);
	}

	// 該方法實現ServletContextAware接口,只在web項目中使用,在ApplicationContextAware的setApplicationContext與BeanPostProcessors的postProcessBeforeInitialization之間
	public void setServletContext(ServletContext arg0) {
		System.out
				.println("***ServletContextAware's setServletContext (only applicable when running in a web application context)***\n"
						+ arg0);
	}

	public Object postProcessBeforeInitialization(Object arg0, String arg1)
			throws BeansException {
		System.out
				.println("***postProcessBeforeInitialization methods of BeanPostProcessors***\n"
						+ arg0 + "\n" + arg1);
		return null;
	}

	public void afterPropertiesSet() throws Exception {
		System.out.println("***InitializingBean's afterPropertiesSet***\n");
	}

	// 自定義的初始化方法
	public void initMethod() {
		System.out.println("***進入初始化方法****");
	}

	public Object postProcessAfterInitialization(Object arg0, String arg1)
			throws BeansException {
		System.out
				.println("***postProcessAfterInitialization methods of BeanPostProcessors***\n"
						+ arg0 + "\n" + arg1);
		return null;
	}

	// 自定義的銷燬方法
	public void destroyMethod() {
		System.out.println("***bean's lifeCycle is over***");
	}

}
    2. applicationContext.xml   
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="springBean"  class="com.lxh.springBeanLifeCycle.SpringBean"
		singleton="true" init-method="initMethod" destroy-method="destroyMethod">
		<property name="name" value="lxh" />
    </bean>		
</beans>
    3. Test.java
package com.lxh.springBeanLifeCycle;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;

public class Test {
	// ApplicationContext
	public static ApplicationContext context = new ClassPathXmlApplicationContext(
			Test.class.getResource("").toString() + "/applicationContext.xml");
	// XmlWebApplication 
	public static XmlWebApplicationContext  context2 = new XmlWebApplicationContext();
	
	// 日誌
	@SuppressWarnings("unused")
	private static Logger logger = Logger.getLogger(Test.class);
	
	public void run(){
		SpringBean sb = (SpringBean) context.getBean("springBean");
		System.out.println("***"+sb+"***");
		// 銷燬bean
   	      ((ClassPathXmlApplicationContext)context).close();
	}
}
    4. index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.lxh.springBeanLifeCycle.Test" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
     <%
     	Test t = new Test();
     	t.run(); 
     %>
</body>
</html>
    5. 測試結果
相關文章
相關標籤/搜索