BeanPostProcessor example

web.xml

 	<bean id="contactBean1" class="com.vf.constructor.objectmatch.Contact"  init-method="init" destroy-method="destroy">
 	<property name="name" value="charles fels"></property>
	</bean>	
	<bean class="com.vf.constructor.objectmatch.initBean" />

InitBean.java

package com.vf.constructor.objectmatch;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class InitBean implements BeanPostProcessor {

	/**
	 * @param args
	 */

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		// TODO Auto-generated method stub
		logger.debug("After Initialization: " + beanName);
		return bean;
	}
	private Logger logger = Logger.getLogger(InitBean.class.getPackage().getName());

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		// TODO Auto-generated method stub
		logger.debug("Before Initialization:" + beanName);
		return bean;
}
}
Contact.java

package com.vf.constructor.objectmatch;

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

public class Contact {

	/**
	 * @param args
	 */
	private String name;
	private int age;
	private static Logger logger = Logger.getLogger(Contact.class.getPackage().getName());
	
	public void init() {
		if(name == null) {
			logger.debug("You are not authenticated.");
			throw new IllegalArgumentException ("Initialization failed.");
		}
		
	}
	
	public void destroy() {
		logger.debug("Destroying starting.");
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	
	public void setAge(int age) {
		this.age = age;
	}
	public int getAge() {
		return age;
	}
	
	public String toString() {
		return "Name: " + name + " Age: " + age;
	}
	
	public static void main(String[] args) {
		AbstractApplicationContext  context = new ClassPathXmlApplicationContext("web.xml");
		Contact contact = context.getBean("contactBean1", Contact.class);
		logger.debug(contact);
		 context.close();
	}
	

}
DEBUG - Creating shared instance of singleton bean 'contactBean1'
DEBUG - Creating instance of bean 'contactBean1'
DEBUG - Eagerly caching bean 'contactBean1' to allow for resolving potential circular references
DEBUG - After Initialization: contactBean1
DEBUG - Invoking init method  'init' on bean with name 'contactBean1'
DEBUG - Before Initialization:contactBean1
DEBUG - Finished creating instance of bean 'contactBean1'
DEBUG - Returning cached instance of singleton bean 'com.vf.constructor.objectmatch.initBean#0'
DEBUG - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@d30557]
DEBUG - Returning cached instance of singleton bean 'lifecycleProcessor'
DEBUG - Returning cached instance of singleton bean 'contactBean1'
DEBUG - Name: charles fels Age: 0
INFO - Closing org.springframework.context.support.ClassPathXmlApplicationContext@18eabf6: startup date [Mon Apr 15 10:01:06 GMT+08:00 2013]; root of context hierarchy
DEBUG - Returning cached instance of singleton bean 'lifecycleProcessor'
INFO - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@166c37e: defining beans [contactBean1,com.vf.constructor.objectmatch.initBean#0]; root of factory hierarchy
DEBUG - Invoking destroy method 'destroy' on bean with name 'contactBean1'
DEBUG - Destroying starting.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值