Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the steps for migrating from Spring to SpringBoot

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What is the method and step of migrating from Spring to SpringBoot? I believe many inexperienced people are at a loss about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Migrate from Spring to Spring Boot

Spring Boot provides a series of conveniences for our development, so we may want to convert an old Spring project into a new Spring Boot project, and this article will explore how to do this.

Please note that Spring Boot does not replace Spring, it just adds something that is automatically configured to make Spring programs faster and better

Add Spring Boot starters

The easiest way to add Spring Boot is to add Spring Boot Starters.

Org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE

Add Application Portal

Every SpringBoot program needs an application entry, usually a main program that uses the @ SpringBootApplication annotation:

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

The @ SpringBootApplication annotation is a combination of the following:

@ Configuration,@ EnableAutoConfiguration,@ComponentScan.

By default, @ SpringBootApplication scans all classes of this package and sub-package. So generally speaking, SpringBootApplication will be placed under the top-level package.

Import Configuration and Components

Spring Boot usually uses automatic configuration, but we can also manually Import existing java configuration or xml configuration.

For existing configurations, we have two options. One is to move these configurations to the same level of package or subpackage of the main Application to facilitate automatic scanning. The second is explicit import.

Let's take a look at how to display imports:

SpringBootApplication@ComponentScan (basePackages= "com.flydean.config") @ Import (UserRepository.class) public class Application {/ /...}

If it is a xml file, you can also use @ ImportResource to import:

@ SpringBootApplication@ImportResource ("applicationContext.xml") public class Application {/ /.}

Migrate application resources

By default, Spring Boot looks for the following resource address:

/ resources/public/static/META-INF/resources

If we want to migrate, we can migrate the existing resources to the address of the appeal, or we can use the following methods:

Spring.resources.static-locations=classpath:/images/,classpath:/jsp/

Migrate application properties files

Spring Boot looks for application.properties or application.yml files in the following places:

* current directory * current directory / configsubdirectory * / config directory in classpath * classpath root

We can move the properties file under the path mentioned above.

Migrate Spring Web programs

If we want to migrate the Spring Web program, we need the following steps:

Add spring-boot-starter-web dependencies:

Org.springframework.boot spring-boot-starter-web

Through the automatic configuration of Spring Boot, the dependent packages in classpath are automatically detected, @ EnableWebMvc is automatically opened, and a DispatcherServlet is created.

If we use the @ EnableWebMvc annotation in the @ Configuration class, the automatic configuration will fail.

The automatic configuration also automatically configures the following three bean:

HttpMessageConverter is used to convert JSON and XML. / error mapping is used to handle all errors / static, / public, / resources or / META-INF/resources static resource support.

Configure the View template

For web pages, JSP is no longer recommended, but is replaced with various template techniques: Thymeleaf, Groovy, FreeMarker, Mustache. All we have to do is add the following dependencies:

Org.springframework.boot spring-boot-starter-thymeleaf

The template file is under / resources/templates.

If we still need to use JSP, we need to display the configuration as follows:

Spring.mvc.view.prefix=/WEB-INF/views/spring.mvc.view.suffix=.jsp

After reading the above, have you mastered the migration steps from Spring to SpringBoot? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report