溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用Spring怎么創(chuàng)建一個(gè)web應(yīng)用

發(fā)布時(shí)間:2021-03-23 15:43:32 來源:億速云 閱讀:168 作者:Leah 欄目:編程語言

使用Spring怎么創(chuàng)建一個(gè)web應(yīng)用?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

Maven 依賴

首先,我們需要引用 spring-boot-starter-web 依賴:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.1.1.RELEASE</version>
</dependency>

該依賴包含:

  • Spring Web 應(yīng)用程序所需的 spring-web 和 spring-webmvc 模塊

  • Tomcat 容器,這樣我們就可以直接運(yùn)行 Web 應(yīng)用程序,而無需安裝 Tomcat

創(chuàng)建一個(gè)Spring Boot 應(yīng)用程序

使用 Spring Boot 的最直接的方法是創(chuàng)建一個(gè)主類,并添加 @SpringBootApplication 注解:

@SpringBootApplication
public class SpringBootRestApplication {
  public static void main(String[] args) {
    SpringApplication.run(SpringBootRestApplication.class, args);
  }
}

此單個(gè)注釋等效于使用 @Configuration ,@EnableAutoConfiguration 和 @ComponentScan 。

默認(rèn)情況下,它將掃描本包和它的子包中的所有組件。

接下來,對(duì)于基于 Java 的 Spring Bean 配置,我們需要?jiǎng)?chuàng)建一個(gè)配置類,并使用 @Configuration 注解:

@Configuration
public class WebConfig {
 
}

該注解是 Spring 主要使用的配置。 它本身使用 @Component 進(jìn)行元注解,這使注解的類成為標(biāo)準(zhǔn) bean,因此也成為組件掃描時(shí)的候選對(duì)象。

讓我們看看使用核心 spring-webmvc 庫的方法。

使用 spring-webmvc

Maven 依賴

首先,我們需要引用 spring-webmvc 依賴:

<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 來設(shè)置默認(rèn)的 Spring MVC 配置,而

@ComponentScan 可以指定用于掃描組件的包。

@EnableWebMvc 注解提供了 Spring Web MVC 配置,比如設(shè)置 dispatcher servlet、啟用 @Controller 和 @RequestMapping 注解以及設(shè)置其他默認(rèn)值。

@ComponentScan 配置組件掃描指令,指定要掃描的包。

初始化類

接下來,我們需要添加一個(gè)實(shí)現(xiàn) WebApplicationInitializer 接口的類:

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 類創(chuàng)建 Spring 上下文,這意味著我們僅使用基于注釋的配置。 然后,我們指定要掃描組件和配置類的包。

最后,我們定義 Web 應(yīng)用程序的入口點(diǎn) — DispatcherServlet 。

此類可以完全替換 < 3.0 Servlet 版本中的 web.xml 文件。

XML配置

讓我們快速看一下等效的XML web配置:

<context:component-scan base-package="com.qulingfeng.controller" />
<mvc:annotation-driven />

我們可以用上面的 WebConfig 類替換這個(gè) XML 文件。
要啟動(dòng)應(yīng)用程序,我們可以使用一個(gè)初始化器類來加載 XML 配置或 web.xml 文件。

關(guān)于使用Spring怎么創(chuàng)建一個(gè)web應(yīng)用問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI