ApplicationContext Resource leak: 'ctx' is never closed

西夏一品堂 发布于 2015/03/15 14:32
阅读 3K+
收藏 0

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

eclipse总是提示这一句

Resource leak: 'ctx' is never closed


但是ApplicationContext ctx里面没有close方法

请问,怎么破

加载中
0
nightmare123
nightmare123


try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml")) 
{
MyBean bean = context.getBean(MyBean.class); 
}
0
小心有诈
小心有诈

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

0
l
lhozy
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() {
		
	}
}



0
BurningDarkness
BurningDarkness

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