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");
    }
}
posted @ 2017-02-07 18:35  WWWYC  阅读(3024)  评论(0编辑  收藏  举报