Spring整合Web開發


 

時間:2017-2-2 02:17

 

——導入jar包

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

    spring-context-3.2.0.RELEASE.jar
    spring-core-3.2.0.RELEASE.jar
    spring-expression-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>
        <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對象。


——示例代碼

Servlet:

public class UserServlet extends HttpServlet {
 
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        UserService userService = (UserService) context.getBean("userService");
        userService.sayHello();
    }
}

----------------------------------------------------------------------------------------------------------------------------

web.xml

<?xml version="1.0" encoding="UTF-8"?>
    <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>com.wyc.servlet.UserServlet</servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/UserServlet</url-pattern>
    </servlet-mapping>
 
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 
    <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"?>
 
    <bean id="userService" class="com.wyc.service.UserService" />
 
</beans>


----------------------------------------------------------------------------------------------------------------------------

UserService:

public class UserService {
    public void sayHello(){
        System.out.println("Hello Spring Web");
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM