您的位置:首页 > 编程语言 > Java开发

Spring基础知识(4)

2016-05-11 17:35 459 查看
一、在web项目中集成Spring

1、直接在Servlet 加载Spring 配置文件

package lsq.service;

public class HelloService {
public void sayHello(){
System.out.println("hello,Spring web");
}
}
package lsq.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lsq.service.HelloService;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//通过spring容器获得HelloService对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService helloService = (HelloService) applicationContext.getBean("helloService");
helloService.sayHello();
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

}
这种做法存在的问题:每次请求都会加载Spring环境,初始化所有Bean ,性能问题 !!!

17:02:45,316  INFO ClassPathXmlApplicationContext:510 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3d94cc41: startup date [Wed May 11 17:02:45 CST 2016]; root of context hierarchy
17:02:45,317  INFO XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [applicationContext.xml]
17:02:45,329  INFO DefaultListableBeanFactory:577 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5c127549: defining beans [helloService]; root of factory hierarchy
hello,Spring web
17:08:36,682  INFO ClassPathXmlApplicationContext:510 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@35757617: startup date [Wed May 11 17:08:36 CST 2016]; root of context hierarchy
17:08:36,684  INFO XmlBeanDefinitionReader:315 - Loading XML bean definitions from class path resource [applicationContext.xml]
17:08:36,698  INFO DefaultListableBeanFactory:577 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@611943bf: defining beans [helloService]; root of factory hierarchy
hello,Spring web
解决方案:

方案一 : 将代码放入Servlet init 中 , 无法保证所有Servlet都能使用 ApplicationContext

方案二 : ServletContext ----- tomcat启动时, 加载Spring配置文件,获得对象 ,放入ServletContext

最适合写这个代码的是:

* ServletCointextListener

2、导入spring-web.jar

保存 ContextLoaderListener 完成在Servlet初始化阶段,加载Spring配置文件,将工厂对象放入 ServletContext

3、配置web.xml

☆注意:* 默认读取 WEB-INF/applicationContext.xml

配置 全局参数 contextConfigLocation 指定 配置文件位置

<!-- 配置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>

4、修改Servlet代码

package lsq.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lsq.service.HelloService;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class HelloServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//通过spring容器获得HelloService对象
//		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

//从ServletContext中获取Spring工厂
//第一种方法:
//		WebApplicationContext applicationContext = (WebApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
//第二种方法:
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

HelloService helloService = (HelloService) applicationContext.getBean("helloService");
helloService.sayHello();
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

}
修改后的效果:

web容器初始化的时候,加载spring配置



当再次访问web容器时,不再重复加载spring配置



二、Spring 整合 junit4 测试

1)导入spring-test.jar

2)编写测试用例

package lsq.test;

import lsq.service.HelloService;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml") //☆:指定配置文件的位置
public class HelloServiceTest {
@Autowired
private HelloService helloService;   //注入需要测试的对象

@Test
public void demo2(){
helloService.sayHello();   //调用测试方法
}

@Test
//没有整合spring的测试
public void demo1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloService helloService = (HelloService) applicationContext.getBean("helloService");
helloService.sayHello();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: