iocmaintest.java

来自「随书光盘:精通Sping 2.0 的随书源代码」· Java 代码 · 共 50 行

JAVA
50 行
字号
package test.ejb3;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.beans.factory.access.SingletonBeanFactoryLocator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * @author worldheart
 *
 */
public class IoCMainTest {

	protected static final Log log = LogFactory.getLog(IoCMainTest.class);
	
	public static void main(String[] args) {

		ApplicationContext ac1 = new ClassPathXmlApplicationContext("ac1.xml"); 

		//ac1作为ac2的双亲
		ApplicationContext ac2 = new ClassPathXmlApplicationContext(new String[]{"ac2.xml"}, ac1);
		
		//ac3作为ac2的双亲
		ApplicationContext ac3 = new ClassPathXmlApplicationContext(new String[]{"ac3.xml"}, ac2);
		
		//从ac3中操控ac1中的受管POJO
		log.info(ac3.getBean("bi1"));
	
		ApplicationContext ac = new ClassPathXmlApplicationContext(
				new String[]{"ac1.xml","ac2.xml","ac3.xml"});
		log.info(ac.getBean("bi1"));
		
		ApplicationContext appcontext = new ClassPathXmlApplicationContext("ac.xml");
		ApplicationContext appcontext1 = (ApplicationContext)appcontext.getBean("appcontext3");
		log.info(appcontext1.getBean("bi1"));

		//SingletonBeanFactoryLocator、ContextSingletonBeanFactoryLocator
		BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance();
		BeanFactoryReference bfr = bfl.useBeanFactory("appcontext3");
		IBusinessInterface bi = (IBusinessInterface)bfr.getFactory().getBean("bi2");
		log.info(bi.getStr("worldheart"));
		
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?