web整合Spring

Spring整合Web開發

時間:2017-2-2 02:17html

 

——導入jar包

一、導入Spring開發基本jar包
    spring-beans-3.2.0.RELEASE.jarjava

    spring-context-3.2.0.RELEASE.jar
    spring-core-3.2.0.RELEASE.jar
    spring-expression-3.2.0.RELEASE.jar
 

二、導入commons-logging.jar

三、導入Spring Web開發jar包
    spring-web-3.2.0.RELEASE.jarweb


——簡單測試

一、編寫一個Service

二、編寫一個Servlet

三、編寫配置文件

四、編寫log4j.properties

五、訪問Servlet調用Service方法

可是在測試的過程當中發現:
    每次執行Servlet的時候都會加載Spring環境,如何解決?
        *   將加載的信息內容保存到ServletContext中,ServletContext對象是全局對象,服務器啓動時就會建立,在建立ServletContext時就會加載Spring環境。
        *   能夠建立一個監聽器:ServletContextListener,用於監聽ServletContext對象的建立和銷燬。

這件事情spring-web-3.2.0.RELEASE.jar幫助咱們完成了。

——配置監聽器

將Spring容器的初始化操做,交由Web容器負責。

一、配置核心監聽器:ContextLoaderListener
    Spring提供的ContextLoaderListener實現了ServletContextListener接口。

二、配置全局參數:contextConfigLocation
    用於指定Spring框架的配置文件的位置。
    默認在XmlWebApplicationContext類中指定爲WEB-INF目錄下:
        public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
    若是須要修改默認目錄,能夠經過初始化參數進行修改:
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>

——得到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對象。
 
 
----log4j配置在src目錄下
log4j.properties

### \u8BBE\u7F6E###
log4j.rootLogger = debug,stdout,D,Espring

### \u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u62AC ###
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%nexpress

### \u8F93\u51FADEBUG \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=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%napache

### \u8F93\u51FAERROR \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=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
}app

/**
* @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

相關文章
相關標籤/搜索