SpringBoot
首先SpringBoot说就是一个javaweb框架,和springmvc非常相似,约定大于配置,you can just run,能迅速开发web应用。
java企业级->javaee->spring->springboot
Springboot默认打包的是jar需要配置打包插件
<build>
<plugins>
<!-- 打包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
springboot maven配置详解
父依赖,一般这个只是对系统环境的一些配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
父依赖的里面还有父依赖,这个是真正的jar包管理
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
springboot web启动依赖,由于已经由父依赖管理,所以不需要进行版本编写
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
@SpringBootApplication项目启动类的作用!!
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
@ComponentScan事用于扫描bean的,并将其注入的容器中。
@SpringBootConfiguration
这个注解的内部:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Configuration 这个注解表明的是一个配置类这个类的里面也定义了一个@ConpontScan表示这个也将将被注入到容器中。
@AutoConfiguration
这个方法跟踪进去有
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
Spring底层注解@import , 给容器中导入一个组件
那么@AutoConfigurationPackage作用:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
Registrat.class是将主启动类下的所有包都扫描
AutoConfigurationImportSelector的作用:
主要有这个类
protected List getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
Assert.notEmpty(configurations, “No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.”);
return configurations;
}
这个类的目的是将所有的META-INF/spring.factories作为组件选择器导入。
这个可能就是主动配置的源头。
所以,自动配置真正实现是从classpath中搜寻所有的META-INF/spring.factories配置文件 ,并将其中对应的 org.springframework.boot.autoconfigure. 包下的配置项,通过反射实例化为对应标注了 @Configuration的JavaConfig形式的IOC容器配置类 , 然后将这些都汇总成为一个实例并加载到IOC容器中。
结论:
-
SpringBoot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值
-
将这些值作为自动配置类导入容器 , 自动配置类就生效 , 帮我们进行自动配置工作;
-
整个J2EE的整体解决方案和自动配置都在springboot-autoconfigure的jar包中;
-
它会给容器中导入非常多的自动配置类 (xxxAutoConfiguration), 就是给容器中导入这个场景需要的所有组件 , 并配置好这些组件 ;
-
有了自动配置类 , 免去了我们手动编写配置注入功能组件等的工作;
SpringApplication
主要做这几件事:
结论:
-
SpringBoot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值
-
将这些值作为自动配置类导入容器 , 自动配置类就生效 , 帮我们进行自动配置工作;
-
整个J2EE的整体解决方案和自动配置都在springboot-autoconfigure的jar包中;
-
它会给容器中导入非常多的自动配置类 (xxxAutoConfiguration), 就是给容器中导入这个场景需要的所有组件 , 并配置好这些组件 ;
-
有了自动配置类 , 免去了我们手动编写配置注入功能组件等的工作;
由原于yaml的一些语法
map和对象一样
obje:
name: onxc
age: 22
map:
k1:
k2:
数组以及list,set的话
list:
-1
-2
有关于yal中值注入到类的方法
使用@Value的方式进行注入
server:
port: 9091
student:
id: 1
name: onexc
age: 22
package cn.chuan.springbott.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Student {
@Value("${student.id}")
private int id;
@Value("${student.name}")
private String name;
@Value("${student.age}")
private int age;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
同时我们可以使用@ConfigurationProperties来注入,但是这中方式相对于@Value比较方便,它可以全局注入,而@Value只能一个个进行注入,但是@ConfigurationProperties的话,它需要对属性进行set赋值。
package cn.chuan.springbott.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
int id;
String name;
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
server:
port: 9091
student:
id: 1
name: onexc
age: 22
person:
id: 2
name: onexc
配置文件加载位置
指定加载配置文件
指定位置加载配置文件
我们还可以通过spring.config.location来改变默认的配置文件位置
项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;这种情况,一般是后期运维做的多,相同配置,外部指定的配置文件优先级最高
讲讲自动配置原理:
这里首先会有一个@ConditionalOnxxx的注解判断其是否存在,如果存在的话,就允许往容器中添加组件
也就是判断改自动配置类是否生效。
如果生效然么如何将其和yml中配置的相结合呢?
比如HttpEncodingAutoConfiguration类,
他会先@EnableConfigurationProperties({HttpProperties.class})
其代码中还会通过构造方法将其注入
public HttpEncodingAutoConfiguration(HttpProperties properties) {
this.properties = properties.getEncoding();
}
所以HttpProperties是什么?
@ConfigurationProperties(
prefix = “spring.http”
)
public class HttpProperties {
}
我们可以看到其和yml配置文件已经结合,但是这个的HttpProperties的话没有加@Componet来交给spring进行管理,但是@EnableConfigurationProperties({HttpProperties.class})
中是@Import({EnableConfigurationPropertiesRegistrar.class}),
所以@EableConfiguration的作用就是将HttpProperties交给Spring容器进行处理。
通过使用debug=true控制台就会显示出哪些自动配置生效
Postive matches:自动配置类启动(正配置)
Negative matches:(没有启动,没有匹配成功的配置类)
Unconditional classes(没有条件的类)