标签:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService helloService = (HelloService) applicationContext.getBean("helloService");
helloService.sayHello();
问题: 每次请求都会加载Spring环境,初始化所有Bean ,性能问题 !!!
解决方案一: 将代码放入Servlet init 中 , 无法保证所有Servlet都能使用 ApplicationContext
解决方案二: ServletContext,在tomcat启动时, 加载Spring配置文件,获得对象 ,放入ServletContext
* ServletContextListener
保存 ContextLoaderListener 完成在Servlet初始化阶段,加载Spring配置文件,将工厂对象放入 ServletContext
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
配置全局参数 contextConfigLocation 指定 配置文件位置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
从ServletContext中获得 Spring工厂
第一种:
WebApplicationContext applicationContext = (WebApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
第二种:
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
publicclassHelloService{
publicvoid sayHello(){
System.out.println("hello,Spring web");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<beanid="helloService"class="cn.itcast.service.HelloService"></bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-appversion="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置Spring 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置Spring配置文件所在位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<display-name></display-name>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>cn.itcast.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
publicclassHelloServletextendsHttpServlet{
publicvoid doGet(HttpServletRequest request,HttpServletResponse response)
throwsServletException,IOException{
WebApplicationContext applicationContext =WebApplicationContextUtils
.getWebApplicationContext(getServletContext());HelloService helloService =(HelloService) applicationContext
.getBean("helloService"); helloService.sayHello();
}
publicvoid doPost(HttpServletRequest request,HttpServletResponse response)
throwsServletException,IOException{
doGet(request, response);
}
}
1、 导入spring-test.jar
2、 编写测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml") // 指定配置文件位置
public class HelloServiceTest {
@Autowired
private HelloService helloService; // 注入需要测试对象
@Test
// 测试
public void demo2() {
helloService.sayHello(); // 调用测试方法
}
}
标签:
原文地址:http://www.cnblogs.com/tangwan/p/4674976.html