你究竟有多了解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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM