1 Pom文件
1.1 spring-boot-starter-parent
表示当前pom文件从spring-boot-starter-parent继承下来,在spring-boot-starter-parent中提供了很多默认配 置,可以简化我们的开发
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<resource.delimiter>@</resource.delimiter>
<maven.compiler.source>${java.version}</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
- 依赖管理spring-boot-dependencies
<properties>
<activemq.version>5.15.9</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.73</appengine-sdk.version>
<artemis.version>2.6.4</artemis.version>
...
</properties>
这样比如使用starter-web的时候就不需要指定版本号
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
这时候将依赖管理的问题放到dependencyManagement中。
官网说明文档见:13.2.2 Using Spring Boot without the Parent POM
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
1.2 打包管理
使用mvn package打包的plugin
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
1.3 Starters
官网见:13.5 Starters
Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
2 YangApplication
2.1 @SpringBootApplication
官网见:18. Using the @SpringBootApplication Annotation
等同于@EnableAutoConfiguration,@ComponentScan和@Configuration
2.2 SpringApplication.run
官网见:23. SpringApplication
3 配置文件
3.1 初步感受
server.port=9090
3.2 yml文件
application.yml
3.3 给属性注入值
public class Person {
private String name;
private int age;
private Date birthday;
private String[] hobbies;
private IDCard idCard;
...
}
public class IDCard {
private int id;
private String number;
}
person:
name: ViolentAyang
age: 21
birthday: 2000
hobbies: [girl,code,travel]
idCard:
id: 1
number: 111
@Component
@ConfigurationProperties(prefix="person")
@Autowired private
Person person;
如果Person类上报错,在Pom文件中加入如下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
4 处理动静态资源
4.1 动态资源
官网见:90.2 Reload Templates without Restarting the Container
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<span style="color:red; font-size:30pt" th:text="${str}">
</span>
</body>
@Controller
@RequestMapping("/yang")
public class YangController {
@RequestMapping("/hello")
public String hello(Model model){
String str="hello spring boot";
model.addAttribute("str",str);
return "test";
}
}
4.2 静态资源
"classpath:/META-INF/resources/"
"classpath:/resources/"
"classpath:/static/"
"classpath:/public/"
WebMvcAutoConfiguration源码分析
WebMvcAutoConfiguration--->WebMvcAutoConfigurationAdapter.addResourceHandlers(xxx)--->
this.resourceProperties.getStaticLocations()
return this.staticLocations;
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
观察
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false) public class ResourceProperties {
配置application.properties
spring.resources.static-locations=classpath:/yang/
|