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"?>

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<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"?>

<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.wyc.service.UserService" />

</beans>

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

UserService:

public class UserService {

public void sayHello(){

System.out.println("Hello Spring Web");

}

}

时间: 2024-08-12 02:49:44

Spring整合Web开发的相关文章

JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试

Spring框架学习路线 Spring的IOC Spring的AOP,AspectJ Spring的事务管理,三大框架的整合 Spring框架概述 什么是Spring? Spring是分层的JavaSE/EE full-stack(一站式)轻量级开源框架. 所谓分层: SUN提供的EE的三层结构:web层.业务层.数据访问层(也称持久层,集成层). Struts2是web层基于MVC设计模式框架. Hibernate是持久的一个ORM的框架. 所谓一站式:Spring框架有对三层的每层解决方案.

Spring整合web开发(6)

正常整合Servlet和Spring没有问题的但是每次执行Servlet的时候加载Spring配置,加载Spring环境. 解决办法:在Servlet的init方法中加载Spring配置文件? 当前这个Servlet可以使用,但是其他的Servlet的用不了了!!! 将加载的信息内容放到ServletContext中.ServletContext对象时全局的对象.服务器启动的时候创建的.在创建ServletContext的时候就加载Spring的环境. ServletContextListene

Springboot 系列(六)Spring Boot web 开发之拦截器和三大组件

1. 拦截器 Springboot 中的 Interceptor 拦截器也就是 mvc 中的拦截器,只是省去了 xml 配置部分.并没有本质的不同,都是通过实现 HandlerInterceptor 中几个方法实现.几个方法的作用一一如下. preHandle 进入 Habdler 方法之前执行,一般用于身份认证授权等. postHandle 进入 Handler 方法之后返回 modelAndView 之前执行,一般用于塞入公共模型数据等. afterCompletion 最后处理,一般用于日

Springboot 系列(五)Spring Boot web 开发之静态资源和模版引擎

前言 Spring Boot 天生的适合 web 应用开发,它可以快速的嵌入 Tomcat, Jetty 或 Netty 用于包含一个 HTTP 服务器.且开发十分简单,只需要引入 web 开发所需的包,然后编写业务代码即可. 自动配置原理? 在进行 web 开发之前让我再来回顾一下自动配置,可以参考系列文章第三篇.Spring Boot 为 Spring MVC 提供了自动配置,添加了如下的功能: 视图解析的支持. 静态资源映射,WebJars 的支持. 转换器 Converter 的支持.

Spring Boot 整合 Web 开发

这一节我们主要学习如何整合 Web 相关技术: Servlet Filter Listener 访问静态资源 文件上传 文件下载 Web三大基本组件分别是:Servlet,Listener,Filter.正常来说一旦我们用了框架,这三个基本就用不上了,Servlet 被 Controller 代替,Filter 被拦截器代替.但是可能在一些特殊的场景下不得不使用这三个基本组件时,Spring Boot 中要如何去引用呢?下面我们来一起学习一下. Spring Boot 集成了 Servlet 容

Spring.DM web开发环境搭建

作为一个初学者来说,搭建好Spring.DM 的web开发环境还是有些麻烦的.我就遇到了N多麻烦,走了很多弯路.本文介绍了2种比较简单的搭建Spring.DM OSGi web开发环境的搭建. 第一种方法 和上一篇文章相似,请移步这里:Spring.DM版HelloWorld 首先引用spring-osgi-2.0.0.M1文件夹中的dist和lib文件夹种的包(不清楚了,请看Spring.DM版HelloWorld 种的环境准备章节),具体引用后的包效果如下: 图1 配置"运行选项”,ecli

Spring在web开发中的应用

(1)在 web 项目中要使用 spring 需要导入一个 jar 包: spring-web-4.2.4.jar包 (2)在 web.xml 文件中配置 Listener 1 <listener> 2 <listener-class> 3 org.springframework.web.context.ContextLoaderListener 4 </listener-class> 5 </listener> 这个 ContextLoaderListen

spring boot web开发

json 接口开发 在以前的spring 开发的时候需要我们提供json接口的时候需要做如下配置: 1 添加jackjson等jar包 2 配置spring controller扫描 3 对接的方法添加@ResponseBody 如果使用spring boot 如何做呢,只需要类添加 @RestController即可,默认类中的方法都以json格式返回. 举例1: /** * @RestController = @Controller + @ResponseBody. 所以,以后定义contr

【Spring环境搭建】在Myeclipse下搭建Spring环境-web开发

<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.s