你究竟有多了解Spring?

 

有句话这么说的:程序员的能力不在于增加代码的能力,而在于减少代码的能力。基于这个我认之为真的命题,我经常问和我一起工作的程序员:你的程序还能不能精简一点?如果能,那能不能再精简一点?

要减少程序,往往建立在程序员对自己的程序代码本身、对业务,以及对所使用的技术框架都非常的熟悉的情况下,而这些就是一个程序员的综合能力。

 

 

前段时间在做Spring培训的时候,我给学员们出了一道题,用于检查学员对Spring了解的深度。各位读者不妨也来试试,题目是这样的:

 

对于以下Github程序:

https://github.com/davenkin/springmvc4-helloworld

对于所有文件,只删除文件或者文件里的某些行,不能修改某行,不能增加行,不能增加文件,在程序运行时,依然能看到页面上的"Hello World!", 能删除文件行或文件最多者胜出.

 

要运行该程序,git clone代码后,将命令行切换到springmvc4-helloworld目录,对于Linux用户,运行:

./gradlew jettyRun

 

对于Windows用户,运行:

gradlew.bat jettyRun

 

初次执行可能会比较慢, 因为gradle会从网上下载很多依赖。之后打开http://localhost:8080/springmvc4-helloworld/hello,你将看到页面上的“Hello World !”。

 

这个程序本身其实是一个很简单的HelloWorld程序,但是却包含了Spring的很多内在工作机制以及框架约定(Convention over Configuration),这些机制和约定没有搞清楚,我就不能说你对Spring了解得有多深。

 

原程序主要的目录结构如下:

src
└── main
    ├── java
    │   └── hello
    │       └── HomeController.java
    ├── resources
    │   └── applicationContext.xml
    └── webapp
        └── WEB-INF
            ├── jsp
            │   └── hello.jsp
            ├── spring-servlet.xml
            └── web.xml

 

一个标准的Java Web项目,web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

    <display-name>hello</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

 

对应的spring-servlet.xml文件如下:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven/>

    <context:component-scan base-package="hello"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>

</beans>

 

另外,还有另一个Spring配置文件applicationContext.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

</beans>

 

我们定义了一个HomeController:

package hello;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class HomeController {

    @RequestMapping(value = "hello", method = RequestMethod.GET)
    public String hello(Model model) {
        model.addAttribute("info", "Hello World!");
        return "/WEB-INF/jsp/hello.jsp";
    }
}

 

该HomeController所对应的View为hello.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
    <c:out value="${info}"/><p>
</body>
</html>

 

posted @ 2016-09-28 12:03  无知者云  阅读(4642)  评论(18编辑  收藏  举报