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")); } }
时间: 2024-10-26 13:34:24