Spring加载applicationContext.xml实现spring容器管理的单例模式
package com.etc.pojo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpUtil { static private ApplicationContext ac; static { ac = new ClassPathXmlApplicationContext("applicationContext.xml"); } private SpUtil() { } public static ApplicationContext getAc() { return ac; } }
package com.etc.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import com.etc.pojo.SpUtil; public class TestDemo { @Test public void test05() { //实现方式四 单例模式 ApplicationContext ac = SpUtil.getAc(); System.out.println(ac.getBean("person")); } }