博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring中bean的生命周期测试---Java project中
阅读量:5954 次
发布时间:2019-06-19

本文共 4652 字,大约阅读时间需要 15 分钟。

hot3.png

    1. SpringBean.java--------spring bean
package com.lxh.springBeanLifeCycle;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;public class SpringBean implements BeanNameAware, BeanClassLoaderAware,		BeanFactoryAware, ResourceLoaderAware, ApplicationEventPublisherAware,		MessageSourceAware, ApplicationContextAware, BeanPostProcessor,		InitializingBean {	// 属性	private String name;	// set,get	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	// spring bean 生命周期	@Override	public void setBeanName(String arg0) {		System.out.println("***BeanNameAware's setBeanName***\n" + arg0);	}	@Override	public void setBeanClassLoader(ClassLoader arg0) {		System.out.println("***BeanClassLoaderAware's setBeanClassLoader***\n"				+ arg0);	}	@Override	public void setBeanFactory(BeanFactory arg0) throws BeansException {		System.out.println("***BeanFactoryAware's setBeanFactory***\n" + arg0);	}	@Override	public void setResourceLoader(ResourceLoader arg0) {		System.out				.println("***ResourceLoaderAware's setResourceLoader (only applicable when running in an application context)***\n"						+ arg0);	}	@Override	public void setApplicationEventPublisher(ApplicationEventPublisher arg0) {		System.out				.println("***ApplicationEventPublisherAware's setApplicationEventPublisher (only applicable when running in an application context)***\n"						+ arg0);	}	@Override	public void setMessageSource(MessageSource arg0) {		System.out				.println("***MessageSourceAware's setMessageSource (only applicable when running in an application context)***\n"						+ arg0);	}	@Override	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之间	// @Override	// public void setServletContext(ServletContext arg0) {	// System.out.println("***ServletContextAware's setServletContext (only applicable when running in a web application context)***\n"	// + arg0);	// }		@Override	public Object postProcessBeforeInitialization(Object arg0, String arg1)			throws BeansException {		System.out				.println("***postProcessBeforeInitialization methods of BeanPostProcessors***\n"						+ arg0 + "\n" + arg1);		return null;	}	@Override	public void afterPropertiesSet() throws Exception {		System.out.println("***InitializingBean's afterPropertiesSet***\n");	}	// 自定义的初始化方法	public void initMethod() {		System.out.println("***进入初始化方法****");	}	@Override	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   

    3. Test.java-------测试类
package com.lxh.springBeanLifeCycle;import org.apache.log4j.Logger;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {	// ApplicationContext	public static ApplicationContext context = new ClassPathXmlApplicationContext(			Test.class.getResource("").toString() + "/applicationContext.xml");	// 日志	@SuppressWarnings("unused")	private static Logger logger = Logger.getLogger(Test.class);	// main	public static void main(String[] args) {		SpringBean sb = (SpringBean) context.getBean("springBean");		System.out.println("***"+sb+"***");		// 销毁bean		((ClassPathXmlApplicationContext)context).close();	}}

运行结果:

转载于:https://my.oschina.net/u/1989867/blog/534776

你可能感兴趣的文章
35个Java代码性能优化总结
查看>>
Spring+SpringMVC+MyBatis+easyUI整合基础篇(一)项目简述及技术选型介绍
查看>>
DFI、DPI技术
查看>>
hibernate 执行存储过程 方法
查看>>
RapidIOIP核的验证方法研究_王玉欢
查看>>
崩溃日志的实例
查看>>
base64是啥原理
查看>>
字符串中去除连续相同的字符保留一个
查看>>
实战 Windows Server 2012 群集共享卷
查看>>
CSS 元素超出部分滚动, 并隐藏滚动条
查看>>
React中那些纠结你的地方(一)
查看>>
Docker入门安装教程
查看>>
PhoneGap极光推送 cordova消息推送
查看>>
Subarray Sum Equals K
查看>>
preventDefault, stopPropagation, stopImmediatePropagation 三者的区别
查看>>
算法题解:找出包含给定字符的最小窗口(枚举的一般方法)
查看>>
超级账本HyperLedger初体验
查看>>
完美解决html中select的option不能隐藏的问题。
查看>>
推荐5大开源工具,用于开发Kubernetes项目
查看>>
制定2015年的移动开发策略
查看>>