概述
注解: 在Spring中盡管使用XML配置文件可以實現Bean的裝配工作,但如果應用中Bean的數量較多,會導致XML配置文件過於臃腫,從而給維護和升級帶來一定的困難。
從JDK 5開始提供了名為Annotation(注解)的功能,Spring正是利用這一特性,Spring逐步完善對Annotation注解技術的全面支持,使XML配置文件不再臃腫,向“零配置”邁進。
Spring框架也為表示層提供了一個優秀的Web框架,即Spring MVC。由於Spring MVC采用了松耦合可插拔組件結構,比其他MVC框架具有更大的擴展性和靈活性。通過注解,Spring MVC使得POJO成為處理用戶請求的控制器,無需實現任何接口。
@
壹:注解說明
Spring中定義了一系列的Annotation注解,如下所示:
| 注解名稱 | 說明 |
|---|---|
| @Component注解 | @Component 是一個泛化的概念,僅僅表示一個組件(Bean),可以作用在任何層次。 |
| @Repository注解 | @Repository 注解用於將數據訪問層(DAO 層)的類標識為Spring的Bean。 |
| @Service注解 | @Service 通常作用在業務層,但是目前該功能與@Component相同。 |
| @Controller注解 | @Controller標識表示層組件,但是目前該功能與@Component相同 |
| @Autowired注解 | 用於對Bean的屬性變量、屬性的set方法及構造函數進行標注,配合對應的注解處理器完成Bean的自動配置工作。@Autowired注解默認按照Bean類型進行裝配。@Autowired注解加上@Qualifier注解,可直接指定一個Bean實例名稱來進行裝配。 |
| @Resource注解 | 作用相當於@Autowired,配置對應的注解處理器完成Bean的自動配置工作。區別在於:①:@Autowired默認按照Bean類型進行裝配,②:@Resource默認按照Bean實例名稱進行裝配。 |
貳:實現注解聲明控制器與請求映射
一:使用controller
org.springframework.stereotype.Controller注解類型用於指示Spring類的實例是一個控制器,其注解形式為@Controller。該注解在使用時不需要再實現Controller接口,只需要將@Controller注解加入到控制器類上,然后通過Spring的掃描機制找到標注了該注解的控制器即可。
@Controller
public class SpringController {
@GetMapping("/helloWorld")
public String hello(){
System.out.println("hello.....");
return "hello";
}
}
我們常用的rest 風格請求(REST : 即 Representational State Transfer 。(資源)表現層狀態轉化):
| 請求 | 說明 | 用於 |
|---|---|---|
| @GetMapping | 匹配GET方式的請求; | 一般讀取數據 |
| @PostMapping | 匹配POST方式的請求; | 一般用於插入數據 |
| @PutMapping | 匹配PUT方式的請求; | 一般用於更新數據 |
| @DeleteMapping | 匹配DELETE方式的請求; | 一般用於刪除數據 |
二:配置包掃描與視圖解析器
1、配置包掃描
雖然哦我們已經i邪惡好了controller,但是直接這樣寫我們是不能用的,還需要在spring-mvc.xml配置文件中,用spring的包掃描將他注入到容器中,我們才能實現調用。
<!--使用掃描機制 -->
<context:annotation-config/>
<context:component-scan base-package="com.lomtom.controller"/>
當然,spring提供了很多種方法,我們是用最簡單實現的就可以。
2、配置試圖解析器
SpringMVC中的視圖解析器的主要作用就是將邏輯視圖轉換成用戶可以看到的物理視圖。
在spring-mvc.xml加入試圖解析器,其中的前綴就是根據自己的文件存放目錄來寫,后綴就是你的文件的后綴名,你可以是.jsp、.html等等。
<!-- 配置視圖解析器 prefix:前綴, suffix:后綴-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".html"></property>
</bean>
三:配置部署描述符
Deployment Descriptors(描述符)是一個xml文件,用來描述如何部署一個模塊或者應用(根據描述符中定義的配置和容器選項)。
在這里簡單來說就是我們的web.xml。
1、讀取spring-mvc.xml文件
雖然,我們已經把controller通過spring-mvc.xml注入到容器中,相信這時你啟動項目時,是訪問不了的controller的請求的,也就是說,我們的我沒在配置該文件,這時候在你的web.xml中加入。
如果我們不指定SpringMVC配置文件的路徑,則會自動到WEB-INF下找 ‘‘前端控制器名-servlet.xml’’ 這個文件,如果找不到則會報錯。
<!-- 配置Spring MVC配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
2、配置匹配映射
servlet: 它提供靜態資源。它處理所有未映射到其他帶有servlet映射的servlet(在這里或在您自己的-web.xml文件)。
servlet-mapping: 當為靜態資源提供服務時,Tomcat將自動生成基於資源文件擴展名的“Content Type”頭,基於這些映射。可以在此處添加其他映射(到應用於所有web應用程序),或在您自己的應用程序的web.xml中部署描述符。
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<!-- ServLet 匹配映射 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
四:建立html文件
新建兩個文件,用於訪問測試,一個index.html,寫一個連接指向href=“helloWorld”,寫另一個hello.html,用於訪問成功后,跳轉到該頁面。
叄:配置tomcat
在這里,作者摸索到了兩種配置tomcat的方法,一種就是本地自個的tomcat,還有一種就是maven提供的tomcat容器。
一:配置本地tomcat
基本上就是這幾步,其中的選擇tomcat目錄省略了,不是很難,添加服務就可以,找不到入口就算了,請不要打我。如果你配置tomcat也不會,那么,現在放下電腦去打把王者榮耀吧,妲己可能會告訴你。

二:配置maven內置tomcat
配置maven的tomcat相對會麻煩一點,不過也不是很麻煩,在你的pom.xml文件中加入一下插件依賴。
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
然后,添加配置,需要注意的是,你需要配置你的maven,在下圖的General里面,如果你已經使用了maven,就不用配了,可以查看General下配置是否正確。
安裝與配置maven:傳送門

接下來你就可以啟動你的項目了,祝你能夠一步成功,哈哈哈哈。
肆:結果及問題
一:tomcat啟動示意圖:
本地tomcat:

maven內置tomcat:

二:結果
首頁:
死案及后跳轉:

三:問題
1、解決SpringMVC不能訪問html頁面
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
2、使用maven內置tomcat有時能跳轉,有時不能跳轉,不能跳轉的時候他會卡在讀取文件這里,這里對不起了,筆者沒找到解決方法,如果你找到了,歡迎告訴筆者。
三月 21, 2020 1:02:09 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-mvc.xml]
該問題追加:
根據csdn評論中大佬的解決方法,我重新試了一下,后來發現問題根源,發現項目根本找不到spring-context.xsd,導致spring-mvc.xml文件加載出錯,並且造成通配符的匹配很全面, 但無法找到元素 'context:annotation-config' 的聲明的錯誤,所以項目一直在加載spring-mvc.xml文件,這時我就意識到是我的spring-mvc.xml中的引入出問題了,經過修改,問題解決了。
修改前:
https://www.springframework.org/schema/context/spring-context.xsd
修改后:
http://www.springframework.org/schema/context/spring-context.xsd
伍:結構及源碼
源碼都放出來了,還不是ctrl+c,ctrl+v一頓亂搞。
一:目錄結構

二:源碼
1、controller
package com.lomtom.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @Author: LOMTOM
* @Date: 2020/3/20
* @Time: 18:40
* @Email: lomtom@qq.com
*/
@Controller
public class SpringController {
@GetMapping("/helloWorld")
public String hello(){
System.out.println("hello.....");
return "hello";
}
}
2、spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven/>
<!--使用掃描機制 -->
<context:annotation-config/>
<context:component-scan base-package="com.lomtom.controller"/>
<!-- 配置視圖解析器 prefix:前綴, suffix:后綴-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".html"></property>
</bean>
</beans>
3、web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<!-- 配置DispatcherServlet -->
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定spring mvc配置文件位置 不指定使用默認情況 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<!-- ServLet 匹配映射 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
4、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring</artifactId>
<groupId>spring</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-8</artifactId>
<packaging>war</packaging>
<name>spring-8 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>spring-8</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
5、html
1、index.html
<html>
<body>
<h2>Hello World!</h2>
<a href="helloWorld">hello</a>
</body>
</html>
2、hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>hello</title>
</head>
<body>
<h2>你好啊,你成功了</h2>
</body>
</html>
作者有話
這篇文章,作者已經肝了很久了,如果對你有用的話,點個贊再走吧,不說了,作者要交作業去了。
