1. Overview
1.概述
The tutorial illustrates how to create a Web Application with Spring.
该教程说明了如何用Spring创建一个Web应用程序。
We’ll look into the Spring Boot solution for building the application and also see a non-Spring Boot approach.
我们将研究构建应用程序的Spring Boot解决方案,也会看到一种非Spring Boot的方法。
We’ll primarily use Java configuration, but also have a look at their equivalent XML configuration.
我们将主要使用Java配置,但也要看一下其对应的XML配置。
2. Setting Up Using Spring Boot
2.使用Spring Boot进行设置
2.1. Maven Dependency
2.1.Maven的依赖性
First, we’ll need the spring-boot-starter-web dependency:
首先,我们需要spring-boot-starter-web依赖性。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.2</version>
</dependency>
This starter includes:
这个启动器包括。
- spring-web and the spring-webmvc module that we need for our Spring web application
- a Tomcat starter so that we can run our web application directly without explicitly installing any server
2.2. Creating a Spring Boot Application
2.2.创建一个Spring Boot应用程序
The most straightforward way to get started using Spring Boot is to create a main class and annotate it with @SpringBootApplication:
开始使用Spring Boot的最直接方法是创建一个主类,并用@SpringBootApplication对其进行注释。
@SpringBootApplication
public class SpringBootRestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootRestApplication.class, args);
}
}
This single annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan.
这个单一的注解等同于使用@Configuration、@EnableAutoConfiguration和@ComponentScan.。
By default, it will scan all the components in the same package or below.
默认情况下,它将扫描同一包内或以下的所有组件。
Next, for Java-based configuration of Spring beans, we need to create a config class and annotate it with @Configuration annotation:
接下来,对于基于Java的Spring Bean配置,我们需要创建一个config类,并用@Configuration注解来注释它。
@Configuration
public class WebConfig {
}
This annotation is the main artifact used by the Java-based Spring configuration; it is itself meta-annotated with @Component, which makes the annotated classes standard beans and as such, also candidates for component-scanning.
这个注解是基于Java的Spring配置所使用的主要工件;它本身是用@Component进行元注解的,这使得被注解的类成为标准Bean,因此也是组件扫描的候选者。
The main purpose of @Configuration classes is to be sources of bean definitions for the Spring IoC Container. For a more detailed description, see the official docs.
@Configuration类的主要目的是成为Spring IoC容器的bean定义的来源。有关更详细的描述,请参阅官方文档。
Let’s also have a look at a solution using the core spring-webmvc library.
我们也来看看使用核心spring-webmvc库的解决方案。
3. Setting Up Using spring-webmvc
3.使用spring-webmvc进行设置
3.1. Maven Dependencies
3.1.Maven的依赖性
First, we need the spring-webmvc dependency:
首先,我们需要spring-webmvc依赖性。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.3</version>
</dependency>
3.2. The Java-based Web Configuration
3.2.基于Java的网络配置
Next, we’ll add the configuration class that has the @Configuration annotation:
接下来,我们将添加具有@Configuration注解的配置类。
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.baeldung.controller")
public class WebConfig {
}
Here, unlike the Spring Boot solution, we’ll have to explicitly define @EnableWebMvc for setting up default Spring MVC Configurations and @ComponentScan to specify packages to scan for components.
在这里,与Spring Boot解决方案不同,我们必须明确定义@EnableWebMvc以设置默认的Spring MVC配置,以及@ComponentScan以指定要扫描组件的包。
The @EnableWebMvc annotation provides the Spring Web MVC configuration such as setting up the dispatcher servlet, enabling the @Controller and the @RequestMapping annotations and setting up other defaults.
@EnableWebMvc注解提供了Spring Web MVC的配置,如设置dispatcher servlet、启用@Controller和@RequestMapping注解以及设置其他默认值。
@ComponentScan configures the component scanning directive, specifying the packages to scan.
@ComponentScan 配置组件扫描指令,指定要扫描的包。
3.3. The Initializer Class
3.3.初始化器类
Next, we need to add a class that implements the WebApplicationInitializer interface:
接下来,我们需要添加一个实现WebApplicationInitializer接口的类:。
public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.scan("com.baeldung");
container.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher =
container.addServlet("mvc", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
Here, we’re creating a Spring context using the AnnotationConfigWebApplicationContext class, which means we’re using only annotation-based configuration. Then, we’re specifying the packages to scan for components and configuration classes.
在这里,我们使用AnnotationConfigWebApplicationContext类创建一个Spring上下文,这意味着我们只使用基于注解的配置。然后,我们指定要扫描组件和配置类的包。
Finally, we’re defining the entry point for the web application – the DispatcherServlet.
最后,我们要定义Web应用程序的入口点–DispatcherServlet.。
This class can entirely replace the web.xml file from <3.0 Servlet versions.
这个类可以完全取代<3.0 Servlet版本中的web.xml文件。
4. XML Configuration
4.XML配置
Let’s also have a quick look at the equivalent XML web configuration:
让我们也快速看一下同等的XML网络配置。
<context:component-scan base-package="com.baeldung.controller" />
<mvc:annotation-driven />
We can replace this XML file with the WebConfig class above.
我们可以用上面的WebConfig类替换这个XML文件。
To start the application, we can use an Initializer class that loads the XML configuration or a web.xml file. For more details on these two approaches, check out our previous article.
为了启动应用程序,我们可以使用一个加载XML配置的Initializer类或一个web.xml文件。关于这两种方法的更多细节,请查看我们之前的文章。
5. Conclusion
5.结论
In this article, we looked into two popular solutions for bootstrapping a Spring web application, one using the Spring Boot web starter and other using the core spring-webmvc library.
在这篇文章中,我们研究了两种流行的引导Spring Web应用的解决方案,一种是使用Spring Boot Web Starter,另一种是使用核心的spring-webmvc库。
In the next article on REST with Spring, I cover setting up MVC in the project, configuration of the HTTP status codes, payload marshalling, and content negotiation.
在下一篇关于使用Spring的REST的文章中,我介绍了在项目中设置MVC、配置HTTP状态代码、有效载荷编排和内容协商。
As always, the code presented in this article is available over on Github. This is a Maven-based project, so it should be easy to import and run as it is.
与往常一样,本文介绍的代码可以在Github上获得。这是一个基于Maven的项目,所以应该很容易导入并按原样运行。