目录
2.@ConfigurationProperties 用于获取配置文件前缀为custom的value值,装配bean
3.@ImportResource:用来加载 xml 配置文件。
pring-boot-autoconfigure 模块用法详解
spring-boot-configuration-processor 依赖
7 @ConditionalOnWebApplication
1.获取全部的配置 (终极配置注解)
@Autowired
Environment env;
@SpringBootApplication
public class ConfigProjectApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigProjectApplication.class, args);
}
@Autowired
Environment env;
@PostConstruct
public void test(){
System.out.println(env.getProperty("spring.profiles.active"));
System.out.println(env.getProperty("xiaoli.zhang"));
}
}
application.properties
spring.profiles.active=dev
xiaoli.zhang=500
获取配置接口
@Value("${spring.profiles.active}")
private String active;
private static final LocalDateTime START_TIME = LocalDateTime.now();
@Autowired
Environment env;
@ApiOperation(value = "test", notes = "test")
@GetMapping("test")
public Map test() {
Map ret = new TreeMap();
ret.put("启动环境",active);
ret.put("启动时间",START_TIME.toString());
ret.put("port", env.getProperty("server.port"));
return ret;
}
2.@ConfigurationProperties 用于获取配置文件前缀为custom的value值,装配bean
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "aliyun")
public class GreenTextScan {
private String accessKeyId;
private String secret;
}
yaml 配置文件
aliyun:
accessKeyId: xxxxxxxxx
secret: yyyyyy
参考: Spring Boot — YAML_springboot yaml_北涯的博客-优快云博客
3.@ImportResource:用来加载 xml 配置文件。
4 @Value:
//Spring boot application.properties 配置的属性的值。示例代码:
@Value("${app.message}")
private String message;
5.
springboot 配置文件优先级
启动时指定环境
java -jar config_project-0.0.1-SNAPSHOT.jar --spring.profiles.active=pro
Jar包启动指定配置文件用法介绍
springboot 自动配置实现
Spring Boot的自动配置原理可以简述为以下几个步骤:
- Spring Boot应用启动时,会通过类路径扫描机制,扫描所有的jar包,找到其中的Spring Boot Starter组件。
- Spring Boot Starter组件中包含了一些预定义的配置类,这些配置类使用Spring的@Configuration注解标记,并通过条件注解@ConditionalOnClass、@ConditionalOnBean等判断条件控制是否需要生效。
- 根据条件判断,自动配置组件会在Spring应用程序启动时自动被装配进Spring容器中。
- 自动配置的组件通常会使用Spring的@ConditionalOnMissingBean注解,如果用户已经显式地定义了同类型的bean,那么自动配置的组件就不会生效,保证了用户自定义配置的优先级。
- 用户可以通过自定义配置类,通过使用Spring的@Import注解导入自定义的配置类来覆盖和扩展默认的自动配置。
6、 提供 Starter 依赖:Spring Boot 还提供了 Starter 依赖,这些 Starter 依赖可以帮助我们快速集成各种常见的第三方库,比如 Spring Boot Starter Web、Spring Boot Starter Data JPA 等。
spring 自定义starter
项目地址: git@gitcode.net:daizikui/mystarter.git
springboot自定义和使用starter(附源码下载)_liuzixin_lzx的博客-优快云博客
实现自定义Spring Boot Starter_spring 引入自定义的starter pom文件_懒虫虫~的博客-优快云博客
pring-boot-autoconfigure 模块用法详解
spring-boot-autoconfigure模块用法详解_java_脚本之家
spring-boot-configuration-processor 依赖
SpringBoot学习:spring-boot-configuration-processor_浅蓝色的幸福的博客-优快云博客
Spring中的常用的条件注解
Spring中的常用的条件注解_spring条件注解_一颗奋起萌发的种子的博客-优快云博客
1 @ConditionalOnMissingBean
2 @ConditionalOnClass
3 @ConditionalOnBean
4 @DependsOn
5 @ConditionalOnProperty
6 @ConditionalOnExpression
7 @ConditionalOnWebApplication
8 @ConditionalOnResource
常用注解
肝了一周总结的SpringBoot常用注解大全,一目了然! - 知乎