1.引入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>
<groupId>first-boot</groupId>
<artifactId>first_boot</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<!-- 指定JDK的版本 -->
<java.version>1.8</java.version>
</properties>
<!-- 声明为一个标准的SpringBoot项目 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<!-- 引入该依赖后会自动引入tomcat和spring-webmvc的包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- 对maven生成的jar进行二次打包,打成一个fat-jar包后,可以直接使用
java -jar xxx.jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.1.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2.创建Application.java类
/**
* @author: krauser
* @date: Create in 下午7:27 2018/4/13
* @Description:
*/
@SpringBootApplication(scanBasePackages = {"org.java.com.*"})
public class Application {
/**
* 1.java -jar
*
* 2.mvn spring-boot:run
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@SpringBootApplication是一个复合注解
public @interface SpringBootApplication {
@AliasFor(
annotation = EnableAutoConfiguration.class
)
Class<?>[] exclude() default {};
@AliasFor(
annotation = EnableAutoConfiguration.class
)
String[] excludeName() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class<?>[] scanBasePackageClasses() default {};
}
3.定义一个简单的controller
@RestController
@RequestMapping("/firstboot")
public class FirstBootController {
@GetMapping(value = "/sayHello")
public String sayHello(){
return "this is my first boot grogram!!!";
}
}
4.现在第三方框架的引入经常会出现jar包冲突,这里有一个查看依赖树的方法,可以查看springboot引入jar包的情况
mvn dependency:tree
[INFO] first-boot:first_boot:jar:1.0-SNAPSHOT
[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.0.1.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter:jar:2.0.1.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot:jar:2.0.1.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.1.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.1.RELEASE:compile
[INFO] | | +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] | | | +- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] | | | \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
[INFO] | | | \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
[INFO] | | \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO] | +- org.springframework:spring-core:jar:5.0.5.RELEASE:compile
[INFO] | | \- org.springframework:spring-jcl:jar:5.0.5.RELEASE:compile
[INFO] | \- org.yaml:snakeyaml:jar:1.19:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-json:jar:2.0.1.RELEASE:compile
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.5:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.9.5:compile
[INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.5:compile
[INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.5:compile
[INFO] | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.5:compile
[INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.1.RELEASE:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.29:compile
[INFO] | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.29:compile
[INFO] | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.29:compile
[INFO] +- org.hibernate.validator:hibernate-validator:jar:6.0.9.Final:compile
[INFO] | +- javax.validation:validation-api:jar:2.0.1.Final:compile
[INFO] | +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
[INFO] | \- com.fasterxml:classmate:jar:1.3.4:compile
[INFO] +- org.springframework:spring-web:jar:5.0.5.RELEASE:compile
[INFO] | \- org.springframework:spring-beans:jar:5.0.5.RELEASE:compile
[INFO] \- org.springframework:spring-webmvc:jar:5.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-aop:jar:5.0.5.RELEASE:compile
[INFO] +- org.springframework:spring-context:jar:5.0.5.RELEASE:compile
[INFO] \- org.springframework:spring-expression:jar:5.0.5.RELEASE:compile
可以看到springboot自动给我们引入了五个包
1. spring-core logback slf4j
2. tomcat相关包
3. hibernate-validator
4. Jackson
5. spring-web spring-webmvc