目录
一、引言
1.1 初始化配置
1.2 整合第三方框架
1.3 后期维护
1.4 部署工程
1.5 敏捷式开发
二、SpringBoot介绍
三、spring boot
3.1 搭建一个spring boot工程
-
新建一个java se的maven工程.
-
spring boot的依赖.
<!-- 注意,这个标签是parent, 表示当前的父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.8</version>
</parent>
-
引入一个组件, spring boot starter web.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
-
创建一个启动类.
/**
* @author 听忆
*/
@SpringBootApplication // 表示我是一个启动类.
public class MyApplication {
// 通过main启动当前的spring boot工程.
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
-
编写一个控制器的方法
/**
* @author 听忆
*/
@RestController
public class UserController {
@GetMapping("/hello")
public String hello(){
return "你好啊,spring boot";
}
}
3.2 使用idea创建项目
3.3 在线创建姿势
注意事项:
-
如果start.spring.io无法访问,「在idea当中」, 则可以尝试去修改idea的配置为:
start.aliyun.com
, 然后按照后续流程进行创建项目. -
目前版本号是: 2.6.8
3.4 项目的目录结构
3.5 项目的运行方式
<!-- 打包插件, 将当前的工程,打包成xxx.jar包。必须得有此插件。 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
注意事项:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited // 四个元注解
------------------------------
@SpringBootConfiguration // sprnig自动装配
@EnableAutoConfiguration // 启动spring 自动装配
// 包扫描配置
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
-------------------------------------------
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {}
以spring mvc为例,查看一下自动装配和我们自定义配置如何做的.
@Configuration(proxyBeanMethods = false)
@Import(EnableWebMvcConfiguration.class)
// 开启配置
@EnableConfigurationProperties({ WebMvcProperties.class, WebProperties.class })
@Order(0)
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware {
@ConfigurationProperties(prefix = "spring.mvc") // 定义一个, 在application.properties文件当中,进行配置的前缀.
// 通过前缀.属性名称 = 值,覆盖当前这个配置类的相关属性.
public class WebMvcProperties {
}
==xxxAutoConfiguration.java==
==xxxProperties.java==
类似于:
ConditionalOnMissingBean
@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
@ConditionalOnClass(RedisOperations.class)
3.6 yml文件格式
也可以配置一些数据或者集合
# 表示配置一个数组或者集合.注意:
# -空格 值
# tingyi.student
tingyi:
student:
- 张三
- 李四
- 王五
3.7 多环境配置
多环境配置的写法总结:
3.8 配置类的使用
如果不写配置类的,可以直接在配置文件当中进行字段相关配置也是可以的.
# 没有配置类,我们直接可以这样配置.
tingyi:
name: 听忆
sex: 男
address: 呀呼
3.9 静态资源处理
public static class Resources {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
/**
* Locations of static resources. Defaults to classpath:[/META-INF/resources/,
* /resources/, /static/, /public/].
*/
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;