YML
-
Brief introduction
-
grammar
-
Configure get
application.ymlperson: name: 小柯 age: 16 date: 2020/01/05 boss: true maps: {k1: v1, k2: w5} list: - aa - bb - cc - dd dog: name: xiaogou age: 15
application.properties
person.name=小王 person.age=100 person.boss=false person.date=2020/06/05 person.list=1,5,6,8 person.maps.aa=123 person.maps.bb=456 person.dog=456
Acquisition (method)
@ConfigurationProperties(prefix = "person") @Component public class Person { private String name; private Integer age; private Date date; private Boolean boss; private Map<String, Object> maps; private List<Object> list; private Dog dog; //getset
Get (Method II)
@Component public class Person { @Value("${person.name}") private String name; @Value("${person.age}") private Integer age; @Value("${person.date}") private Date date; @Value("${person.boss}")
-
@ConfigurationProperties and @Value difference
@PropertySource和@ImportResource
-
@PropertySource load the specified file location
@PropertySource(value = {"classpath:person.properties"}) @ConfigurationProperties(prefix = "person") @Component public class Person { private String name; private Integer age; private Date date; private Boolean boss; private Map<String, Object> maps; private List<Object> list; private Dog dog; //GetSet
-
@ImportResource import Spring configuration file, so that the configuration file to take effect
1, create bean.xml
2, create Service
3, test
4, false resultspackage springboot.io; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootStartApplicationTests { @Autowired ApplicationContext applicationContext; @Test public void testHelloService(){ boolean helloService = applicationContext.containsBean("helloService"); System.out.println(helloService); } }
plus 5, the boot class: @ImportResource (locations = { "CLASSPATH: bean.xml"})
. 6, in place of the test is successful configuration class
7, springboot not recommended @ImportResource introduced using a full injection configuration
8, delete bean.xml, delete @ImportResource notes on startup class
9, (a way) to create a configuration class HelloConfig
use @Configuration and @Bean
(Mode 1) using @Servicepackage springboot.io.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springboot.io.service.HelloService; import springboot.io.service.impl.HelloServiceImpl; @Configuration public class HelloConfig { @Bean public HelloService helloService(){ return new HelloServiceImpl(); } }
Profile of placeholders
- Symbols: $ {}
- Can write the random number {random.uuid}
- Value can be obtained previously configured, if there is no default can be set
$ {person.age: 20}
Many environmental profile
- Default application.properties
- Use other environmental spring.profiles.active = dev
- Project start: - spring.profile.active = dev added to the Program arguments in
- 命令行:java -jar xxx.jar --spring.profile.active=dev
- 虚拟机参数 -Dspring profiles.avtive=dev 加入到VM options
配置文件之加载位置
- 可以加载的位置(优先级由高到低,到优先级会覆盖低优先级配置,前面配置文件没有的,后面的会补上)
file:./config file:./ classpath:./config classpath:./ - spring.config.location来改变默认的配置文件位置
使用条件:项目打包好之后,启动项目改变默认配置文件位置,且与默认配置文件互补。
使用方式:java -jar xxx.jar --spring.config.location=D:/xiaoke.properties
自动配置原理
- 启动主配置@SpringBootApplication
- 开启注解:@EnableAutoConfiguration
- 导入@Import(EnableAutoConfigurationImportSelector.class)
父类:AutoConfigurationImportSelector
List<String> configurations = getCandidateConfigurations(annotationMetadata,attributes);List<String> configurations = SpringFactoriesLoader.loadFactoryNames( getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
SpringFactoriesLoader.loadFactoryNames()扫描所有jar包类路径下META-INF/spring.factories, 把扫描的这些文件内容包装成properties对象,封装到result中返回
- 获取META-INF/spring.factories
public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) { String factoryClassName = factoryClass.getName(); try { Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories"); ArrayList result = new ArrayList(); while(urls.hasMoreElements()) { URL url = (URL)urls.nextElement(); Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url)); String factoryClassNames = properties.getProperty(factoryClassName); result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames))); } return result; } catch (IOException var8) { throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() + "] factories from location [" + "META-INF/spring.factories" + "]", var8); } }
-
以HttpEncodingAutoConfiguration为例
//表示这是一个配置类,以前编写的配置文件一样,也可以给容器中添加组件 @Configuration //启动指定类,@EnableConfigurationProperties功能:将配置文件中对应的值和HttpEncodingProperties绑定起来 @EnableConfigurationProperties(HttpEncodingProperties.class) //判断当前应用是否是web应用,如果是,当前配置类生效 @ConditionalOnWebApplication //判断当前项目有没有这个类,CharacterEncodingFilter:springMvc中进行乱码解决的过滤器 @ConditionalOnClass(CharacterEncodingFilter.class) //判断配置文件中是否存在某个配置,spring.http.encoding.enabled;如果不存在判断是成立的 //即使我们配置文件中不配置spring.http.encoding.enabled = true 也是默认生效的 @ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true) public class HttpEncodingAutoConfiguration {
-
debug = true 在控制台打印出那些自动配置类生效;
-
怎么解析视图的,后面慢慢理解