Spring整合Web开发

 

时间:2017-2-2 02:17java

 

——导入jar包

一、导入Spring开发基本jar包
    spring-beans-3.2.0.RELEASE.jarweb

    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.jarspring


——简单测试

一、编写一个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对象。


——示例代码

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");
    }
}
相关文章
相关标签/搜索