web程序测试时如何加载spring配置文件中的bean

2024-12-01 13:44:58
推荐回答(1个)
回答1:

ApplicationContext获取上下文。
(1)在java文件中:主要用于jUnit测试1导包:2importorg.springframework.context.ApplicationContext;3importorg.springframework.context.support.ClassPathXmlApplicationContext;4获取bean:5ApplicationContext context =newClassPathXmlApplicationContext("beans.xml");6UserDao userDao = (UserDao) context.getBean("userDao", UserDao.

在jsp文件中测试:1引用相关类:2<%@pageimport="org.springframework.web.context.WebApplicationContext"%3<%@pageimport="org.springframework.web.context.support.WebApplicationContextUtils"%4获取bean:5WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this