4
回答
ApplicationContext Resource leak: 'ctx' is never closed
终于搞明白,存储TCO原来是这样算的>>>   

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

eclipse总是提示这一句

Resource leak: 'ctx' is never closed


但是ApplicationContext ctx里面没有close方法

请问,怎么破

举报
共有4个答案 最后回答: 2年前


try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml")) 
{
MyBean bean = context.getBean(MyBean.class); 
}

那你看看ClassPathXmlApplicationContext里有没有, 应该是有的, 然后把 ctx 的类型换成ClassPathXmlApplicationContext

ApplicationContext ctx声明为成员变量
public class UserServiceTest {
	
	private static UserService us;
	private static ApplicationContext ctx;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		 ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		 us=(UserService) ctx.getBean("userService");
	}

	@Test
	public void testSave() {
		
	}
}




ConfigurableApplicationContext 扩展于 ApplicationContext,新增加两个主要方法:refresh() 和 close(), 让 ApplicationContext 具有启动、刷新和关闭上下文的能力。ClassPathXmlApplicationContext,FileSystemXmlApplicationContext继承ConfigurableApplicationContext 
顶部