Spring整合Web开发
时间:2017-2-2 02:17
——导入jar包
1、导入Spring开发基本jar包
spring-beans-3.2.0.RELEASE.jar
2、导入commons-logging.jar
3、导入Spring Web开发jar包
spring-web-3.2.0.RELEASE.jar
——简单测试
1、编写一个Service
2、编写一个Servlet
3、编写配置文件
4、编写log4j.properties
5、访问Servlet调用Service方法
但是在测试的过程中发现:
每次执行Servlet的时候都会加载Spring环境,如何解决?
* 将加载的信息内容保存到ServletContext中,ServletContext对象是全局对象,服务器启动时就会创建,在创建ServletContext时就会加载Spring环境。
* 可以创建一个监听器:ServletContextListener,用于监听ServletContext对象的创建和销毁。
这件事情spring-web-3.2.0.RELEASE.jar帮助我们完成了。
——配置监听器
将Spring容器的初始化操作,交由Web容器负责。
1、配置核心监听器:ContextLoaderListener
Spring提供的ContextLoaderListener实现了ServletContextListener接口。
2、配置全局参数:contextConfigLocation
用于指定Spring框架的配置文件的位置。
默认在XmlWebApplicationContext类中指定为WEB-INF目录下:
public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
如果需要修改默认目录,可以通过初始化参数进行修改:
<param-name>contextConfigLocation</param-name>
——获得WebApplicationContext对象
因为Spring容器已经交由Web容器初始化和管理,所以获得WebApplicationContext对象需要依赖ServletContext对象:
通常直接在Servlet中获取:
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
底层也是通过:getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);来获得。
另一种获取方式:
WebApplicationContext context = (WebApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
通常使用第一种方式来获得ApplicationContext对象。
### u8BBEu7F6E###
log4j.rootLogger = debug,stdout,D,E
### u8F93u51FAu4FE1u606Fu5230u63A7u5236u62AC ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### u8F93u51FADEBUG u7EA7u522Bu4EE5u4E0Au7684u65E5u5FD7u5230=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E://study_fold/logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### u8F93u51FAERROR u7EA7u522Bu4EE5u4E0Au7684u65E5u5FD7u5230=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://study_fold/logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
——示例代码
Servlet:
public class UserServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
Logger log = Logger.getLogger(UserServlet.class);
/**
* Default constructor.
*/
public UserServlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
WebApplicationContext con = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
UserService us = (UserService) con.getBean("userService");
response.getWriter().println(us.getName());
us.sayHello();
for(int i=0; i<10; i++)
{
log.debug("debug message..."+i);
log.error("debug message..."+i);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
----------------------------------------------------------------------------------------------------------------------------
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.donghua.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/UserServlet</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
----------------------------------------------------------------------------------------------------------------------------
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="userService" class="com.donghua.UserService">
<property name="name" value="Test Name"/>
</bean>
</beans>
----------------------------------------------------------------------------------------------------------------------------
UserService:
public class UserService
{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void sayHello()
{
System.out.println("Hello Spring Web STE");
}
}
浏览器输入:http://localhost:8083/spring-hibernate/UserServlet
Test Name