1、pom配置方式
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.springboot</groupId> 7 <artifactId>quick_start</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>quick_start</name> 12 <url>http://maven.apache.org</url> 13 <description>Demo project for Spring Boot</description> 14 15 <properties> 16 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 17 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 18 <maven.compiler.source>1.8</maven.compiler.source> 19 <maven.compiler.target>1.8</maven.compiler.target> 20 <java.version>1.8</java.version> 21 </properties> 22 23 <dependencies> 24 <!--不使用parent方式进行依赖,需要scope和type设置--> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-dependencies</artifactId> 28 <version>2.0.2.RELEASE</version> 29 <scope>import</scope> 30 <type>pom</type> 31 </dependency> 32 33 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-web</artifactId> 37 </dependency> 38 39 <dependency> 40 <groupId>org.springframework.boot</groupId> 41 <artifactId>spring-boot-starter-test</artifactId> 42 <scope>test</scope> 43 </dependency> 44 </dependencies> 45 46 <build> 47 <plugins> 48 <plugin> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-maven-plugin</artifactId> 51 </plugin> 52 </plugins> 53 </build> 54 55 56 </project>
2、实体类编写
1 package com.springboot; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.context.ConfigurableApplicationContext; 6 import org.springframework.context.annotation.Bean; 7 8 /****************************** 9 * @Author : liuyang 10 * @ClassName : QuickStartApplication 11 * @Date : 2018 五月 20 12 * @Time : 01:05:59 13 * @Type : SpringBoot 14 * @Version : 1.0 15 * @Return : 16 * @Description : 17 *******************************/ 18 19 @SpringBootApplication 20 public class QuickStartApplication { 21 22 @Bean 23 public Runnable createRunnable() { 24 25 return () -> { 26 System.out.println("Spring Boot is Run"); 27 }; 28 29 } 30 31 public static void main(String[] args) { 32 ConfigurableApplicationContext applicationContext = SpringApplication.run(QuickStartApplication.class,args); 33 applicationContext.getBean(Runnable.class).run(); 34 } 35 }
源码存放:
需要将parent部分注释掉
https://github.com/liushaoye/quick_start
这个在官方文档上也有具体写:
https://docs.spring.io/spring-boot/docs/2.0.2.RELEASE/reference/htmlsingle/#boot-documentation
仔细阅读这一段就是具体解答