最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

Spring MVC官方文档学习笔记(一)之Web入门

来源:博客园

注: 该章节主要为原创内容,为后续的Spring MVC内容做一个先行铺垫

1.Servlet的构建使用


(资料图)

(1) 选择Maven -> webapp来构建一个web应用

(2) 构建好后,打开pom.xml文件,一要注意打包方式为war包,二导入servlet依赖,如下

war      javax.servlet    javax.servlet-api    3.1.0    provided  

(3) 替换webapp/WEB-INF/web.xml文件为如下内容,采用Servlet 3.1版本

  

(4) 在main目录下,新建java目录和resources目录,并在java目录下新建包,最终项目目录结构如下

(5) 编写一个简单的servlet如下

@WebServlet("/example")public class ExampleServlet extends HttpServlet {    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        resp.getWriter().write("example...");    }}

(6) 有了servlet后,我们得让服务器知道哪个请求要交给哪个servlet处理,因此还需要配置web.xml如下

                exampleServlet        cn.example.springmvc.boke.servlet.ExampleServlet                        exampleServlet        /    

(7) 然后,为了能够在网页上访问,我们得把这个项目部署到tomcat服务器中

首先,在URL栏中,添加上项目名称,此处为springmvc

然后,在Deployment中添加我们的项目

最后,注意 Application Context 中的值,应与前面在URL栏中添加的项目名称相同,此处均为springmvc

(8) 最后,启动tomcat服务器,在浏览器上输入 http://localhost:8080/springmvc/example ,如果能看到 example... 字符串,则说明项目配置成功

2.基于web.xml,整合Spring与Servlet

(1) 现在,web应用已经搭建好了,但是我们希望能够在该应用中使用Spring容器,该怎么办呢? 在之前的非web环境中,我们都是在main方法中创建ioc容器(如 new ClassPathXmlApplicationContext()),然后直接使用的,但是现在没有了main方法,该由谁来创建ioc容器呢? 答案就是由我们的web容器,可以在web应用初始化的时候来帮助我们创建,但创建好之后,我们该怎么获取到ioc容器呢? Servlet规定了4大作用域,分别为page域(PageContext),当前页面有效; request域(HttpServletContext),一次请求内有效; session域(HttpSession),一次会话内有效; application域(ServletContext),在当前整个web应用内有效,因此我们可以将创建好的ioc容器直接放到application域中,这样在任何位置,我们都能拿到ioc容器进行使用,具体示例如下

首先导入相关的Spring依赖包

      org.springframework    spring-context    5.2.22.RELEASE        org.springframework    spring-web    5.2.22.RELEASE  

接着,修改我们的代码,配置一个普通的bean

//创建一个普通的java类public class ExampleService {    public String get() {        return "user";    }}//然后,在resources目录下新建一个springmvc.xml,并将上面的ExampleService注册为一个bean    

接下来,我们就得让web容器来为我们创建ioc容器了,具体由谁来创建呢? Servlet有三大核心组件,即Servlet,用于处理请求;Filter,过滤器,用来拦截或修改请求;Listener,监听器,用于监听某个事件。显然,这里使用Listener最合适,那就由Listener来为我们创建ioc容器

               org.springframework.web.context.ContextLoaderListener                    contextConfigLocation        classpath:springmvc.xml                exampleServlet        cn.example.springmvc.boke.servlet.ExampleServlet                    exampleServlet        /    

现在ioc容器有了,而且被Spring以WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE为key放到了application域中,现在我们可以在任何地方被获取到它,如下所示

@WebServlet("/example")public class ExampleServlet extends HttpServlet {    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //获取application域中的ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE属性值,即我们的ioc容器        XmlWebApplicationContext ctx = (XmlWebApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);        //或者也可以使用Spring提供的工具类WebApplicationContextUtils来获取ioc容器,如下        //XmlWebApplicationContext ctx = (XmlWebApplicationContext) WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());        //使用ioc容器,获取其中的bean        ExampleService exampleService = ctx.getBean(ExampleService.class);        resp.getWriter().write(exampleService.get());    }}//最后,重新启动容器,访问 http://localhost:8080/springmvc/example,会发现页面上出现 user 字符串

当然,向上面这样每次都通过get方法获取,很麻烦,我们可以借助Spring提供的工具类,在Servlet初始化的时候对Servlet进行依赖注入,如下

@WebServlet(urlPatterns = "/example")public class ExampleServlet extends HttpServlet {        //使用@Autowired注解标注需要进行依赖注入的bean    @Autowired    private ExampleService exampleService;        //Servlet初始化方法    @Override    public void init(ServletConfig config) throws ServletException {        super.init(config);        //获取application域        ServletContext servletContext = config.getServletContext();        //使用Spring提供的自动注入工具类SpringBeanAutowiringSupport,直接进行依赖注入        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, servletContext);    }    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        resp.getWriter().println(exampleService.get());    }}

3.基于Servlet扩展接口,整合Spring与Servlet

(1) 在上一节中,我们将ioc的创建配置于web.xml中,但此外我们还可以利用java代码的方式来创建ioc容器,可通过Servlet 3.0提供的ServletContainerInitializer接口,来在web容器启动的时候为第三方组件提供初始化的机会(例如注册Servlet等),如果要使用ServletContainerInitializer接口,那么就必须要在项目或所其依赖的jar包中的/META-INF/services目录下创建一个名称为javax.servlet.ServletContainerInitializer 的文件,而这个文件的具体内容,就是ServletContainerInitializer实现类的全限定类名称,然后,借助java的SPI技术,web容器便会加载这些实现类,通常情况下,ServletContainerInitializer这个接口通常会配合@HandlesTypes注解一起使用,而这个@HandlesTypes注解的作用就是让web容器收集我们项目中所有所指定的类,然后将这些类作为ServletContainerInitializer的onStartup方法参数传入,这样,在web容器启动的时候,我们就可以拿到这些我们所需的类然后创建它们

当然,同上面web.xml中的ContextLoaderListener,Spring也提供了一个ServletContainerInitializer接口的实现类SpringServletContainerInitializer,来创建帮助我们简化ioc容器的创建,首先在spring-mvc jar包中,就定义了一个/META-INF/services/javax.servlet.ServletContainerInitializer文件,然后,在启动时,web容器便会加载这个文件,读取里面的内容,为SpringServletContainerInitializer这个类

由于在SpringServletContainerInitializer上有注解@HandlesTypes标注,而这个注解的值为WebApplicationInitializer,因此,在创建SpringServletContainerInitializer对象前,web容器会收集应用内所有WebApplicationInitializer接口的实现类,并将它们作为参数传递给onStartup方法中的webAppInitializerClasses,这样,在web容器启动时,我们就能初始化我们所指定的对象

总而言之,在应用启动时,web容器会调用ServletContainerInitializer实现类(这里为SpringServletContainerInitializer)中的onStartup方法,而这个onStartup方法中又调用了@HandlesTypes注解所指定的类或接口(此处为WebApplicationInitializer)的实现类中的onStartup方法,因此,我们可以编写一个WebApplicationInitializer的实现类,来创建ioc容器,不过,Spring已经为我们提供了一个实现了WebApplicationInitializer接口的抽象类AbstractContextLoaderInitializer,它里面已经封装好了大部分的逻辑(比如将ioc容器置于application域中等),而我们所需要做的仅仅就是创建一下ioc容器而已,如下

public class IocInit extends AbstractContextLoaderInitializer {    @Override    protected WebApplicationContext createRootApplicationContext() {        XmlWebApplicationContext ctx = new XmlWebApplicationContext();        ctx.setConfigLocation("classpath:springmvc.xml");        return ctx;    }}

此外,不要忘了注释掉web.xml中关于Spring的相关内容,否则会产生产生两个ioc容器

            exampleServlet        cn.example.springmvc.boke.servlet.ExampleServlet                exampleServlet        /    

最后,重启项目,输入http://localhost:8080/springmvc/example,看见user字符串则说明成功

4.Spring MVC

现在,我们将Servlet与Spring ioc容器整合到了一起,但如果我们需要处理新的请求的话,我们还得继承HttpServlet来编写新的Servlet,并将其配置到web.xml中,非常麻烦,因此,Spring变为我们提供了一个全新的框架 - Spring MVC来帮助我们进行开发

关键词: