[java]52、SpringBoot基本配置
1、简介
javaEE开发存在的问题:配置繁多,部署复杂,集成第三方库麻烦
SpringBoot可以很好地解决上述问题
1.1、配置并运行SpringBoot
1、pom.xml文件中配置
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<!-- web项目已经集成了SpringMVC中很多的常用库-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
spring-boot-starter-parent统一定义配置、统一依赖及版本
spring-boot-starter-web提供了web开发的所有依赖项
2、controller配置
@RestController
public class TestController {
@GetMapping("/test")
public String test() {
return "Hellow SpringBoot";
}
}
3、程序入口配置@SpringBootApplication
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Appliaction 在浏览器中输入网址即可启动运行(SpringBoot内置了tomact)
dependency
@SpringBootApplication包含了三个注解1)、
@EnableAutoConfiguration可以根据
naven依赖自动构建相关环境(比如为spring-boot-starter-web构建web容器环境等)2)、
@ComponentScan默认会扫描当前包以及子包中的所有文件
3)、
@SpringBootConfiguration从
SpringBoot2.2.1开始,被SpringBoot扫描到的@Component都不用再加@Configuration
1.2、可运行的Jar
1、可以在pom.xml文件中添加插件maven package打包可生成的jar文件
<build>
<finalName>sb_hw</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.4.RELEASE</version>
</plugin>
</plugins>
</build>
打包完成之后,cd到当期目录,在终端运行java -jar sb_hw.jar
2、也可以使用插件的run功能,直接双击运行
spring-boot:run
1.3、热部署
1、添加依赖(在Debug模式下,可监听classpath的变化)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
每次只需要build当前模块就可以更新内容
2、自动编译
自动编译
Ctrl + Shifter + Alt + /
2、配置文件
2.1、应用程序的配置文件
SpringBoot默认会去加载一个应用程序的配置文件,文件名为application
application配置文件默认可以存放到如下位置(优先级从高到低)
$ROOT_PATH:./config/
$ROOT_PATH:./config/*
$ROOT_PATH:./
classpath:./config/
classpath:./
$ROOT_PATH:./代码项目的根路径
2.2、运行参数(Program Arguments)、VM(Add VM options)选项
可以通过运行参数或VM选项,指定配置文件的文件名、位置。
配置参数
上述配置会读取到
classpath下面的sj.properties文件。
properties文件名称
1、
Program Arguments参数是通过main函数的args参数传递。参数值
--spring.config.name=sj
args
public class Application {
public static void main(String[] args) {
System.out.println("---------");
for (String arg: args) {
System.out.println(arg);
}
SpringApplication.run(Application.class, args);
}
}
2、Add VM options参数可以通过以下代码获取到
参数值-Dspring.config.name=sj -Dage=777
public class Application {
public static void main(String[] args) {
System.out.println("---------");
System.getProperties().forEach((k, v) -> {
System.out.println(k + "_" + v);
});
SpringApplication.run(Application.class, args);
}
}
2.3、配置文件内容
关于服务器的配置参考
spring-boot-autoconfigure.jar/META_INF/spring.factories
ServletWebServerFactoryAutoConfiguration
2.4、配置文件可以注入到类中
将age和name注入到类中
@PropertySource("classpath:sj.properties")
public class TestController {
@Value("${name}")
private String name;
@Value("${age}")
private Integer age;
@GetMapping("/test")
public String test() {
return "test!!!" + name + age;
}
}
sj.properties
applicationContext文件是默认加载到类中,直接通过@Value取即可
2.5、YAML
YAML(YAML Ain't a Markup Language,YAML是配置文件)配置文件的扩展名为.yml
yml
注意事项:
1、
YAML是使用空格或者tab作为层级缩进2、冒号:与后面紧跟的内容之间必须要有空格或者
tab3、字符串可以不用加引号。如果有转义字符,可以使用双引号括住字符串
2.5.1、属性绑定(JavaBean properties binding)
为了更快捷地完成绑定工作,可以添加2个依赖
<!-- 配置文件属性名提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<!-- 在编译期间帮助生成Getter、Setter等代码-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
spring-boot-configuration-processor需要代码重新编译并且重启IDEA才能生效
spring-boot-configuration-processor
在IDEA插件中搜索Lombok,并安装
Lombok
1、
Bean属性和配置文件间的映射
@Component
@ConfigurationProperties("person")
@Data
public class Person {
private Integer id;
private Integer age;
private String name;
private String [] nickNames;
private List<Dog> dogs;
@Data
public static class Dog {
private Integer age;
private String name;
}
}
配置文件内容
2、使用了
@EnableConfigurationProperties后Student就不用加@Component
@ConfigurationProperties("person")
@Data
public class Person {
private Integer id;
private Integer age;
private String name;
private String [] nickNames;
private List<Dog> dogs;
@Data
public static class Dog {
private Integer age;
private String name;
}
}
@RestController
@EnableConfigurationProperties(Person.class)
public class TestController {
@Autowired
private Person person;
@GetMapping("/test")
public String test() {
return "test!!!";
}
}
2.5.2、构造方法绑定
构造方法绑定需要使用注解:@ConstructorBinding
构造方法绑定只能用@EnableConfigurationProperties,不能使用@Component
@ConfigurationProperties("cat")
@ConstructorBinding
@Data
public class Cat {
private Integer id;
private String name;
public Cat(Integer id, String name) {
this.id = id;
this.name = name;
}
}
@RestController
@EnableConfigurationProperties({Person.class,Cat.class})
public class TestController {
@Autowired
private Cat cat;
@GetMapping("/test")
public String test() {
System.out.println(cat);
return "test!!!";
}
}
2.5.3、@Bean的使用
@EnableConfigurationProperties也可以用在@Bean上
@Data
public class Car {
}
@RestController
public class TestController {
@Bean
@ConfigurationProperties("car")
public Car car() {
return new Car();
}
}
2.6、宽松绑定
配置文件中属性名格式比较宽松,例如:
abc.my-project.cat.first-name
2.7、拆分配置文件
1、可以通过---将配置文件拆分成多个文档
通过spring.profiles.active指定需要加载的文档
spring:
profiles:
active: development
---
spring:
profiles: development
jdbc:
url: jdbc:mysql://localhost:3306/test
username: root1
password: root1
---
spring:
profiles: production
jdbc:
url: jdbc:mysql://localhost:3306/shop
username: root2
password: root2
2、文件名application-*的后半部分默认就是文档名
application.yml
person:
age: 11
name: Person-${person.age}
spring:
profiles:
active: [cat, student]
application-cat.yml
cat:
age:10
application-student.yml
student:
age: 20
name: Student-${student.age}
2.8、生成Banner
在resources文件夹下创建banner.txt文件输入要展示的banner,也可以到网站生成banner
更多设置请参考文档
banner