Using Spring Boot without the Parent POM
example:
1.parent 工程 pom文件
<properties>
<springboot.version>2.2.6.RELEASE</springboot.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Override Spring Data release train provided by Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.当前工程 pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.limiaogithub</groupId>
<artifactId>springboot2-learning</artifactId>
<version>1.0</version>
</parent>
<packaging>jar</packaging>
<artifactId>chapter2</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. 编写application.java
package com.github.examples;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
@ResponseBody
@RequestMapping("/hello")
public String hello() {
return "Hello World";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
3.运行
Connected to the target VM, address: '127.0.0.1:52067', transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
2020-04-03 11:17:59.051 INFO 1600 --- [ main] com.github.examples.DemoApplication : Starting DemoApplication on DESKTOP-GLVVPCF with PID 1600 (D:\03github\springboot2-learning\chapter2\target\classes started by limiao in D:\03github\springboot2-learning)
2020-04-03 11:17:59.066 INFO 1600 --- [ main] com.github.examples.DemoApplication : No active profile set, falling back to default profiles: default
2020-04-03 11:18:02.066 INFO 1600 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-04-03 11:18:02.097 INFO 1600 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-04-03 11:18:02.097 INFO 1600 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2020-04-03 11:18:02.879 INFO 1600 --- [ main] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2020-04-03 11:18:02.879 INFO 1600 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-04-03 11:18:02.879 INFO 1600 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3531 ms
2020-04-03 11:18:02.957 INFO 1600 --- [ main] o.s.boot.web.servlet.RegistrationBean : Servlet dispatcherServlet was not registered (possibly already registered?)
2020-04-03 11:18:07.540 INFO 1600 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-04-03 11:18:08.050 INFO 1600 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html]
2020-04-03 11:18:08.810 INFO 1600 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-04-03 11:18:08.820 INFO 1600 --- [ main] com.github.examples.DemoApplication : Started DemoApplication in 11.061 seconds (JVM running for 17.483)