【SpringBoot】SpringBoot基础知识
一、SpringBoot介绍
百度百科:
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
SpringBoot优点
二、搭建SpringBoot项目
2.1 通过官网自动生成
https://start.spring.io/ 快速生成
2.2 IDEA 在线模板生成
通过IDEA提供的在线模板,自动生成SpringBoot项目
2.3 通过maven项目构建
第二点是自动构建项目,该点是手动构建项目,以简单的web应用为例
-
添加依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.15.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
-
添加启动器
package com.example.springbootdemo; import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootDemoApplication { public static void main(String[] args) { //第一种启动方式 //SpringApplication.run(SpringBootDemoApplication.class, args); //第二种启动方式 SpringApplication sa = new SpringApplication(SpringBootDemoApplication.class); //启动 sa.run(args); } }
三、SpringBoot中的常见配置
3.1 入口类和相关注解
这是程序的启动类,main方法的本质其实就是完成一个SpringIoC容器的初始化操作
@SpringBootApplication
public class SpringbootDemoApplication {
public static void main(String[] args) {
// Spring IoC 容器的初始化
ApplicationContext ac = SpringApplication.run(GpSpringbootDemo02Application.class, args);
}
}
@SpringBootApplication
注解是一个组合注解,具体内容是
@Target({ElementType.TYPE}) // 注解可以写在哪些地方
@Retention(RetentionPolicy.RUNTIME) // 该注解的作用域 RESOURCES CLASS RUNTIME
@Documented // 该注解会被API抽取
@Inherited // 可继承
// 以上四个是Java中提供的元注解
@SpringBootConfiguration // 本质上就是一个Configuration注解
@EnableAutoConfiguration // 自动装配的注解
@ComponentScan( // 扫描 会自动扫描 @SpringBootApplication所在的类的同级包(com.gupaoedu)以及子包中的Bean,所有一般我们建议将入口类放置在 groupId+artifcatID的组合包下
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
3.2 Banner
在SpringBoot项目启动的时候,会出现如下内容
这就是程序启动的banner,这部分内容是可以自由设计的,只要在resources
文件夹下创建一个banner.txt
稳健,在文件中写入需要展示的banner图标就可以实现自定义banner
3.2.1 自定义Banner
比如在该网址中输入Hello
创建相关文件,并输入以上复制的内容
3.2.2关闭Banner
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(SpringbootDemoApplication.class);
// 关闭Banner,需要配置在run方法之前,否则不生效
springApplication.setBannerMode(Banner.Mode.OFF);
springApplication.run(args);
}
3.3常见配置
在SpringBoot中给我们提供的有两个配置文件
applicationContext.properties
,applicationContext.yml
这两个配置文件作用是一样的,只是书写格式有些区别,一个项目中只需要其中的一个就可以了。
简单示例
-
Tomcat配置修改
server.port=8082 server.servlet.context-path=/springboot
-
中文乱码配置
server.tomcat.uri-encoding=UTF-8 spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true spring.http.encoding.force=true spring.messages.encoding=UTF-8
-
自定义配置内容
# 自定义的配置信息 user.username=zhangsan user.age=25 user.address=江苏南京
在实体类中获取内容
//使用@Value注解 @Value("${user.username}") private String userName; @Value("${user.age}") private Integer age; @Value("${user.address}") private String address;
导入配置文件内的数据时,需要加上如下依赖
<!--导入配置文件处理器,配置文件进行绑定就会有提示--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
还有一种方法,不使用
@Value
也可以实现配置文件中内容的输出使用
@ConfigurationProperties
注解package com.gupaoedu.bean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component // 属性文件中的属性和User对象中的成员变量映射 @ConfigurationProperties(prefix = "user") public class User { private String username; private Integer age; private String address; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", age=" + age + ", address='" + address + '\'' + '}'; } }