为了方便多环境适配,springboot简化了profile功能。
比如,生产环境、测试环境各自有各自的配置文件,切换不同环境时只要告诉springboot切换到哪个配置文件即可。
默认配置文件 application.yaml;任何时候都会加载
指定环境配置文件: application-{env}.yaml
激活指定环境方式:
配置文件激活
spring:
profiles:
active: ... # 只需写 - 后的名称,如application-prod.yaml => prod。
命令行激活:
java -jar xxx.jar --spring.profiles.active=prod --person.name=haha
修改配置文件的任意值,命令行优先
规则:
@Configuration(proxyBeanMethods = false)
@Profile("production") //此时此类 当环境为 production时才生效
public class ProductionConfiguration {
// ...
}
========================application配置文件============================================
spring.profiles.group.production[0]=proddb
spring.profiles.group.production[1]=prodmq
# 使用:--spring.profiles.active=production 激活
spring.profiles.active=production
外部化配置:将代码中的配置信息绑定到外部文件中就称为外部化配置。
springboot官方说明了外部化配置的规则:
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config
- Default properties (specified by setting
SpringApplication.setDefaultProperties
).@PropertySource
annotations on your@Configuration
classes. Please note that such property sources are not added to theEnvironment
until the application context is being refreshed. This is too late to configure certain properties such aslogging.*
andspring.main.*
which are read before refresh begins.- Config data (such as
application.properties
files)- A
RandomValuePropertySource
that has properties only inrandom.*
.- OS environment variables.
- Java System properties (
System.getProperties()
).- JNDI attributes from
java:comp/env
.ServletContext
init parameters.ServletConfig
init parameters.- Properties from
SPRING_APPLICATION_JSON
(inline JSON embedded in an environment variable or system property).- Command line arguments.
properties
attribute on your tests. Available on@SpringBootTest
and the test annotations for testing a particular slice of your application.@TestPropertySource
annotations on your tests.- Devtools global settings properties in the
$HOME/.config/spring-boot
directory when devtools is active.
外部配置源(springboot可查找外部配置的位置)
比如使用@Value注解
@Value("${person.name}") //可以获取配置文件的值 @Value("${JAVA_HOME}") //也可以获取环境变量的值 @Value("${os.name}") //也可以获取操作系统的名字
配置文件查找位置:(优先级:后面的会覆盖前面的)
classpath 根路径
classpath 根路径下config目录
jar包当前目录
jar包当前目录的config目录
/config子目录的直接子目录
配置文件加载顺序:
指定环境优先,外部优先(后面的可以覆盖前面的同名配置项)
starter的pom文件中引入 autoconfigurer 包
autoconfigure包中配置使用 META-INF/spring.factories 中 EnableAutoConfiguration 的值,使得项目启动加载指定的自动配置类(这是核心,springboot启动时会扫描指定路径下的类)
编写自动配置类 xxxAutoConfiguration -> xxxxProperties
引入starter — xxxAutoConfiguration — 容器中放入组件 ---- 绑定xxxProperties ---- 配置项
举例:
atguigu-hello-spring-boot-starter(启动器): maven模块
pom文件中加:自动配置包的maven依赖
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.atguigugroupId>
<artifactId>atguigu-hello-spring-boot-starterartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>com.atguigugroupId>
<artifactId>atguigu-hello-spring-boot-starter-autoconfigureartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
dependencies>
project>
打包
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jf5l5j0G-1627625678765)(D:\笔记\typroa_images\image-20210612144740138.png)]
atguigu-hello-spring-boot-starter-autoconfigure(自动配置包): springboot模块
此项目不单独打包使用。所以test包、主程序类、配置类都可以删掉。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kIglfYD8-1627625678767)(D:\笔记\typroa_images\image-20210612142543084.png)]
pom文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.6version>
<relativePath/>
parent>
<groupId>com.atguigugroupId>
<artifactId>atguigu-hello-spring-boot-starter-autoconfigureartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>atguigu-hello-spring-boot-starter-autoconfigurename>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
dependencies>
project>
写业务逻辑组件
/**
* 默认不要放在容器中
*/
public class HelloService {
@Autowired
private HelloProperties helloProperties;
public String sayHello(String userName) {
return helloProperties.getPrefix() + ": " + userName + "》" + helloProperties.getSuffix();
}
}
@ConfigurationProperties("atguigu.hello") //绑定配置文件
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
写自动配置类
@Configuration
@EnableConfigurationProperties(HelloProperties.class) //开启属性文件绑定功能,并将该组件HelloProperties放在容器中
public class HelloServiceAutoConfiguration {
@ConditionalOnMissingBean(HelloService.class)
@Bean
public HelloService helloService() {
HelloService helloService = new HelloService();
return helloService;
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gzJ47j79-1627625678768)(D:\笔记\typroa_images\image-20210612143818051.png)]
配置使用 META-INF/spring.factories 中 EnableAutoConfiguration
的值,使得项目启动加载指定的自动配置类
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XEEoDoZ4-1627625678770)(D:\笔记\typroa_images\image-20210612150620104.png)]
#Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
hello.auto.HelloServiceAutoConfiguration
然后打包,供启动器使用。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zV0gAbYx-1627625678771)(D:\笔记\typroa_images\image-20210612144623573.png)]
然后别的项目就可以导入maven依赖使用该启动器了。
步骤:
引入
<dependencies>
<dependency>
<groupId>com.atguigugroupId>
<artifactId>atguigu-hello-spring-boot-starterartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
测试:
@RestController
public class HelloContoller {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String hello() {
return helloService.sayHello("小明");
}
}
配置文件:
atguigu.hello.prefix=ATGUIGU
atguigu.hello.suffix=你好
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UurI8vda-1627625678772)(D:\笔记\typroa_images\image-20210612153724597.png)]
Spring原理【Spring注解】、SpringMVC原理、自动配置原理、SpringBoot原理
public static void main(String[] args) {
SpringApplication.run(XXXX.class, args);
}
创建 SpringApplication
============================SpringApplication===================================
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
、、、
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));//保存一些信息。
this.webApplicationType = WebApplicationType.deduceFromClasspath();//判定当前应用的类型。
this.bootstrapRegistryInitializers = this.getBootstrapRegistryInitializersFromSpringFactories();
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = this.deduceMainApplicationClass();
}
保存一些信息。
判定当前应用的类型(工具类:ClassUtils)。类型例如:Servlet
bootstrapRegistryInitializers:初始启动引导器(说明了项目一启动要干什么)(List)
找 ApplicationContextInitializer;
找 ApplicationListener ;应用监听器。
运行 SpringApplication
============================SpringApplication===================================
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();//StopWatch
stopWatch.start();//记录应用的启动时间
DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();//创建引导上下文(Context环境)
ConfigurableApplicationContext context = null;
this.configureHeadlessProperty();//让当前应用进入headless模式。
SpringApplicationRunListeners listeners = this.getRunListeners(args);//获取所有RunListener(运行监听器)
listeners.starting(bootstrapContext, this.mainApplicationClass);
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args); //保存命令行参数
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);//准备环境
this.configureIgnoreBeanInfo(environment);
Banner printedBanner = this.printBanner(environment);
context = this.createApplicationContext();//创建IOC容器
context.setApplicationStartup(this.applicationStartup);
this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);//准备ApplicationContext IOC容器的基本信息
this.refreshContext(context);//刷新IOC容器
this.afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
listeners.started(context);
this.callRunners(context, applicationArguments);
} catch (Throwable var10) {
this.handleRunFailure(context, var10, listeners);
throw new IllegalStateException(var10);
}
try {
listeners.running(context);
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}
StopWatch
记录应用的启动时间
创建引导上下文(Context环境)createBootstrapContext()
public interface Bootstrapper {
/**
* Initialize the given {@link BootstrapRegistry} with any required registrations.
* @param registry the registry to initialize
*/
void intitialize(BootstrapRegistry registry);
}
让当前应用进入headless模式。java.awt.headless
获取所有** RunListener**(运行监听器)【为了方便所有Listener进行事件感知】
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-byq6KuBo-1627625678773)(D:\笔记\typroa_images\1607004823889-8373cea4-6305-40c1-af3b-921b071a28a8.png)]
保存命令行参数;ApplicationArguments
准备环境 (prepareEnvironment();)
返回或者创建基础环境信息对象。StandardServletEnvironment
配置环境信息对象。
绑定环境信息
创建IOC容器(createApplicationContext())
准备ApplicationContext IOC容器的基本信息 prepareContext()
保存环境信息
IOC容器的后置处理流程。
应用初始化器;applyInitializers;
遍历所有的 ApplicationContextInitializer 。调用 initialize.。来对ioc容器进行初始化扩展功能
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Pi9Murnb-1627625678774)(D:\笔记\typroa_images\1607005958877-bf152e3e-4d2d-42b6-a08c-ceef9870f3b6.png)]
遍历所有的 listener 调用 contextPrepared。EventPublishRunListenr;通知所有的监听器contextPrepared
所有的监听器 调用 contextLoaded。通知所有的监听器 contextLoaded;
**刷新IOC容器。**refreshContext
容器刷新完成后工作?afterRefresh
如果以上有异常,
调用所有监听器的 running 方法 listeners.running(context); 通知所有的监听器 running
**running如果有问题。继续通知 failed 。**调用所有 Listener 的 **failed;**通知所有的监听器 failed
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D8BAGhE9-1627625678775)(D:\笔记\typroa_images\1607006112013-6ed5c0a0-3e02-4bf1-bdb7-423e0a0b3f3c.png)]
@FunctionalInterface
public interface ApplicationRunner {
/**
* Callback used to run the bean.
* @param args incoming application arguments
* @throws Exception on error
*/
void run(ApplicationArguments args) throws Exception;
}
@FunctionalInterface
public interface CommandLineRunner {
/**
* Callback used to run the bean.
* @param args incoming main method arguments
* @throws Exception on error
*/
void run(String... args) throws Exception;
}
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-application-events-and-listeners
ApplicationContextInitializer
ApplicationListener
SpringApplicationRunListener
e CommandLineRunner {
/**
* Callback used to run the bean.
* @param args incoming main method arguments
* @throws Exception on error
*/
void run(String... args) throws Exception;
}
## 2. 自定义事件监听组件
- Application Events and Listeners
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-application-events-and-listeners
**ApplicationContextInitializer**
**ApplicationListener**
**SpringApplicationRunListener**
- ApplicationRunner 与 CommandLineRunner