测试spring中的org.springframework.beans.factory.InitializingBean

MyInitBean.java

import org.springframework.beans.factory.InitializingBean;

public class MyInitBean implements InitializingBean {

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("调用InitializingBean的afterPropertiesSet()...");
	}
}


TestInitBean.java

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.TestCase;


public class TestInitBean extends TestCase {
	private BeanFactory bf;
	
	protected void setUp() {
		bf = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	
	public void testLifeCycle() throws Exception {   
		MyInitBean hello = (MyInitBean) bf.getBean("myInitBean");
    }
}


applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
<beans>  
    <bean id="myInitBean" class="MyInitBean" >
    </bean>  
</beans>

你可能感兴趣的:(java,spring,xml,bean,JUnit)