In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What are the two ways to quickly create web applications using Spring? For this problem, this article details the corresponding analysis and solutions, hoping to help more partners who want to solve this problem find simpler and easier ways.
introduced
The following describes how to use Spring to develop a Web application. We'll look at developing a web application with Spring Boot, and we'll look at non-Spring Boot approaches. We'll primarily use Java configurations, but also understand their XML equivalents.
using Spring Boot
Maven dependency
First, we need to reference the spring-boot-starter-web dependency:
org.springframework.boot spring-boot-starter-web 2.1.1.RELEASE
This dependency includes:
Spring-web and spring-webmvc modules required for Spring Web applications Tomcat container so we can run Web applications directly without installing Tomcat
Creating a Spring Boot Application
The most straightforward way to use Spring Boot is to create a main class and add the @SpringBootApplication annotation:
@SpringBootApplicationpublic class SpringBootRestApplication { public static void main(String[] args) { SpringApplication.run(SpringBootRestApplication.class, args); }}
This single annotation is equivalent to using @Configuration,@EnableAutoConfiguration, and @ComponentScan.
By default, it scans all components in this package and its child packages.
Next, for Java-based Spring Bean configuration, we need to create a configuration class and use the @Configuration annotation:
@Configurationpublic class WebConfig { }
This annotation is the configuration that Spring primarily uses. It itself is meta-annotated with @Component, which makes the annotated class a standard bean and therefore a candidate for component scanning.
Let's look at ways to use the core spring-webmvc library.
Using spring-webmvc
Maven dependency
First, we need to reference the spring-webmvc dependency:
org.springframework spring-webmvc 5.0.0.RELEASE
Java-based Web Configuration
@Configuration@EnableWebMvc@ComponentScan(basePackages = "com.qulingfeng.controller")public class WebConfig { }
Unlike Spring Boot, we must explicitly define @EnableWebMvc to set the default Spring MVC configuration.
@ComponentScan can specify packages to scan for components.
The @EnableWebMvc annotation provides Spring Web MVC configuration, such as setting dispatcher servlets, enabling @Controller and @RequestMapping annotations, and setting other defaults.
@ComponentScan configures the component scan command, specifying the package to scan.
initialize class
Next, we need to add a class that implements the WebApplicationInitializer interface:
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("/"); }}
Here, we use the AnnotationConfigWebApplicationContext class to create the Spring context, which means we only use annotation-based configurations. We then specify packages to scan for components and configuration classes.
Finally, we define the entry point for the Web application- DispatcherServlet.
This class completely replaces the web.xml file in Servlet version < 3.0.
XML configuration
Let's take a quick look at the equivalent XML web configuration:
We can replace this XML file with the WebConfig class above. To launch the application, we can use an initializer class to load the XML configuration or web.xml file.
We looked at two popular ways to develop Spring Web applications, one using the Spring Boot Web launcher and the other using the core spring-webmvc library.
What are the two ways to quickly create web applications using Spring? The answers to the questions are shared here. I hope the above content can be helpful to everyone. If you still have a lot of doubts, you can pay attention to the industry information channel to learn more.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.