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

How to install SpringBoot based on Maven

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

Share

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

Most people do not understand the knowledge points of this article "based on Maven how to install SpringBoot", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to install SpringBoot based on Maven" article.

1. Brief introduction to SpringBoot

Spring Boot is a new framework provided by the Pivotal team, which is designed to simplify the initial building and development process of Spring applications. The framework is configured in a specific way so that developers no longer need to define a templated configuration. Spring Boot can easily create a production application that is based on Spring and runs independently.

2. SpringBoot installation

There are many ways to install SpringBoot, and the simplest one is to add files such as spring-boot-*.jar directly to the project's classpath, so you can run and debug your application. For enterprise applications, it is recommended to use build tools to manage project dependencies, such as Maven or Gradle. As for the way to add classpath directly, there is no further introduction here, but the installation instructions based on the build tool are mainly described below.

2.1. Maven-based installation

Spring Boot is compatible with Apache Maven 3.2. If you haven't already installed Maven, you can follow the instructions on maven.apache.org.

Spring Boot relies on using org.springframework.boot as the groupId. Typically, your POM file inherits the spring-boot-starter-parent project and then declares one or more Starters dependencies. Spring Boot also provides an optional Maven plug-in to generate executable Jar packages.

A typical pom.xml file:

4.0.0 com.example myproject org.springframework.boot spring-boot-starter-parent 1.5.3.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin

Tip:

Spring-boot-starter-parent is a good way to use Spring Boot, but it is not suitable for all scenarios. Sometimes, you may need to inherit a different parent Pom, or you may not like this default configuration. You can also use it by declaring a scope=import dependency:

Org.springframework.boot spring-boot-dependencies 1.5.3.RELEASE pom import 2.2. Gradle-based installation

I prefer Gradle to Maven's huge XML configuration, which is not only concise but also much more flexible. Spring Boot is compatible with Gradle (2.9 or above) and Gradle 3. If you have not already installed Gradle, you can follow the instructions on www.gradle.org to install it.

A typical build.gradle file:

Plugins {id 'org.springframework.boot' version' 1.5.3.RELEASE'id 'java'} jar {baseName =' myproject' version = '0.0.1 SM SNAPSHOT'} repositories {jcenter () maven {url "http://maven.aliyun.com/nexus/content/groups/public/"}} dependencies {compile (" org.springframework.boot:spring-boot-starter-web ") testCompile (" org.springframework.boot:spring-boot-starter-test ")} 3. Create a HelloWorld application

Create a simple Java class as follows:

Import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;@RestController@EnableAutoConfigurationpublic class Example {@ RequestMapping ("/") String home () {return "Hello World!";} public static void main (String [] args) throws Exception {SpringApplication.run (Example.class, args);}}

The first annotation @ RestController on the Example class is a combination of @ Controller and @ ResponseBody, indicating that this is a controller and that all response data is serialized into the response body and returned. The @ RequestMapping annotation on the method provides routing information for request processing. These annotations are Spring-specific, not Spring Boot-specific.

The second annotation @ EnableAutoConfiguration on the class tells Spring Boot to configure the Spring project through the dependent Jar package. Because spring-boot-starter-web contains Tomcat and Spring MVC, it will think that this is a Web project and configure Spring accordingly.

The last part of the application is a main method that delegates the application to Spring Boot by calling the run method of SpringApplication. SpringApplication will guide our application to start Spring and automatically configure the Tomcat Web service. We need to pass Example.class as an argument to the run method to tell SpringApplication that it is the main Spring component.

After running the main method, you can use a browser to open the address localhost:8080 and get the output:

Hello World!

4. Code structure

General applications will use the inversion of the domain name + the project name as the project's uniform package name prefix. Such as: com.qchery.funda. In SpringBoot projects, the following layout is recommended:

Com

+-qchery

+-funda

+-Application.java

| |

+-entity

| | +-Customer.java |

| |

+-dao

| | +-CustomerRepository.java |

| |

+-service

| | +-CustomerService.java |

| |

+-web

+-CustomerController.java

The Application.java file is as follows:

Import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@EnableAutoConfiguration@ComponentScanpublic class Application {public static void main (String [] args) {SpringApplication.run (Application.class, args);}}

In general, we will place the @ EnableAutoConfiguration annotation on the main class, that is, Application.java in the above structure. It implicitly defines search packages that contain certain search terms. For example, if you are writing an JPA application, classes with @ EnableAutoConfiguration annotations will be used to search for classes with @ Entity.

@ ComponentScan is used to automatically scan the corresponding components. Since Application.java is under the root package name of, it does not need to declare basePackage to specify the package to be scanned. The default is the root package name, that is, com.qchery.funda.

5. Configuration class

SpringBoot supports both Java-based configuration and XML file configuration. However, it is recommended to use Java-based configuration. If some XML configuration already exists in the system, it is recommended to replace it with Java-based configuration with the same effect. You can search Enable* to find the corresponding Java-based configuration.

5.1. Import other configuration

In the SpringBoot project, we don't need to put all the configurations in one class file. Its @ Import annotation can help us import other configuration classes. If you want to import a configuration in the form of XML, you can use the @ ImportResource annotation. Alternatively, you can use the @ ComponentScan annotation to scan all classes with @ Configuration annotations.

6. Automatic configuration

When you annotate @ EnableAutoConfiguration or @ SpringBootApplication to a class with @ Configuration, SpringBoot automatically completes the project configuration based on the dependency package you add. For example, if you add a package for HSQLDB under the classpath, but you do not configure any database properties, SpringBoot will automatically configure an In-Memory database. @ EnableAutoConfiguration can only be added to one class, and it is recommended that you add it to the main @ Configuration class.

6.1. Gradually replace the automated configuration

Automated configuration is not intrusive, and you can replace it anywhere by customizing the configuration. For example, if you add your own database configuration, the default embedded database support will be returned. If you want to see if the automation configuration works, you can check it with the-debug parameter when you run the program.

6.2. Disable specific automated configuration

When you find that automated configurations don't work the way you want, you can disable them through the exclude attribute of @ EnableAutoConfiguration.

Import org.springframework.boot.autoconfigure.*;import org.springframework.boot.autoconfigure.jdbc.*;import org.springframework.context.annotation.*;@Configuration@EnableAutoConfiguration (exclude= {DataSourceAutoConfiguration.class}) public class MyConfiguration {} 7. Use @ SpringBootApplication annotations

Because in most cases, @ Configuration,@EnableAutoConfiguration and @ ComponentScan annotations are used together. SpringBoot provides a more convenient choice of @ SpringBootApplication, which is equivalent to the first three annotations.

Import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication / / is equivalent to @ Configuration @ EnableAutoConfiguration @ ComponentScanpublic class Application {public static void main (String [] args) {SpringApplication.run (Application.class, args) }} the above is about the content of this article on "how to install SpringBoot based on Maven". I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

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