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
)
@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
功能,直接双击运行
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
注意事项:
1、
YAML
是使用空格或者tab
作为层级缩进2、冒号:与后面紧跟的内容之间必须要有空格或者
tab
3、字符串可以不用加引号。如果有转义字符,可以使用双引号括住字符串
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
才能生效
在IDEA
插件中搜索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
更多设置请参考文档
网友评论