介紹
本篇文章主要介紹,如何使用 Spring 開(kāi)發(fā)一個(gè) Web 應(yīng)用橱健。
我們將研究用 Spring Boot 開(kāi)發(fā)一個(gè) web 應(yīng)用,并研究用非 Spring Boot 的方法网缝。
我們將主要使用 Java 配置,但還要了解它們的等效的 XML 配置扼仲。
使用 Spring Boot
Maven 依賴(lài)
首先,我們需要引用 spring-boot-starter-web 依賴(lài):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
該依賴(lài)包含:
- Spring Web 應(yīng)用程序所需的 spring-web 和 spring-webmvc 模塊
- Tomcat 容器栖秕,這樣我們就可以直接運(yùn)行 Web 應(yīng)用程序只壳,而無(wú)需安裝 Tomcat
創(chuàng)建一個(gè)Spring Boot 應(yīng)用程序
使用 Spring Boot 的最直接的方法是創(chuàng)建一個(gè)主類(lèi),并添加 @SpringBootApplication 注解:
@SpringBootApplication
public class SpringBootRestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootRestApplication.class, args);
}
}
此單個(gè)注釋等效于使用 @Configuration 暑塑,@EnableAutoConfiguration 和 @ComponentScan 吼句。
默認(rèn)情況下,它將掃描本包和它的子包中的所有組件事格。
接下來(lái)惕艳,對(duì)于基于 Java 的 Spring Bean 配置,我們需要?jiǎng)?chuàng)建一個(gè)配置類(lèi)驹愚,并使用 @Configuration 注解:
@Configuration
public class WebConfig {
}
該注解是 Spring 主要使用的配置远搪。 它本身使用 @Component 進(jìn)行元注解,這使注解的類(lèi)成為標(biāo)準(zhǔn) bean逢捺,因此也成為組件掃描時(shí)的候選對(duì)象谁鳍。
讓我們看看使用核心 spring-webmvc 庫(kù)的方法。
使用 spring-webmvc
Maven 依賴(lài)
首先,我們需要引用 spring-webmvc 依賴(lài):
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
基于 java 的 Web 配置
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.qulingfeng.controller")
public class WebConfig {
}
在這里與 Spring Boot 的方式不同棠耕,我們必須顯式定義 @EnableWebMvc 來(lái)設(shè)置默認(rèn)的 Spring MVC 配置余佛,而 @ComponentScan 可以指定用于掃描組件的包。
@EnableWebMvc 注解提供了 Spring Web MVC 配置窍荧,比如設(shè)置 dispatcher servlet、啟用 @Controller 和 @RequestMapping 注解以及設(shè)置其他默認(rèn)值恨憎。
@ComponentScan 配置組件掃描指令蕊退,指定要掃描的包。
初始化類(lèi)
接下來(lái)憔恳,我們需要添加一個(gè)實(shí)現(xiàn) WebApplicationInitializer 接口的類(lèi):
public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.scan("com.qulingfeng");
container.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher =
container.addServlet("mvc", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
在這里瓤荔,我們使用 AnnotationConfigWebApplicationContext 類(lèi)創(chuàng)建 Spring 上下文,這意味著我們僅使用基于注釋的配置钥组。 然后输硝,我們指定要掃描組件和配置類(lèi)的包。
最后程梦,我們定義 Web 應(yīng)用程序的入口點(diǎn) — DispatcherServlet 点把。
此類(lèi)可以完全替換 < 3.0 Servlet 版本中的 web.xml 文件。
XML配置
讓我們快速看一下等效的XML web配置:
<context:component-scan base-package="com.qulingfeng.controller" />
<mvc:annotation-driven />
我們可以用上面的 WebConfig 類(lèi)替換這個(gè) XML 文件屿附。
要啟動(dòng)應(yīng)用程序郎逃,我們可以使用一個(gè)初始化器類(lèi)來(lái)加載 XML 配置或 web.xml 文件。
結(jié)束語(yǔ)
在篇文章中挺份,我們研究了兩種用于開(kāi)發(fā) Spring Web 應(yīng)用程序的流行方式褒翰,一種使用 Spring Boot Web 啟動(dòng)程序,另一種使用核心 spring-webmvc 庫(kù)匀泊。
歡迎關(guān)注我的公眾號(hào):曲翎風(fēng)优训,獲得獨(dú)家整理的學(xué)習(xí)資源和日常干貨推送。
如果您對(duì)我的專(zhuān)題內(nèi)容感興趣各聘,也可以關(guān)注我的博客:sagowiec.com