@Value注解的原理

1.AutowiredAnnotationBeanPostProcessor是主要逻辑类,基本逻辑同@Autowird

2.PropertySourcesPlaceholderConfigurer中会将StringValueResolver添加到beanFactory

3.外部配置文件properties会加载到ConfigurableEnvironment中,具体逻辑是通过ApplicationEvent事件通知的形式实现的

ConfigFileApplicationListener(最新版本是ConfigDataEnvironmentPostProcessor)是主要的实现类,它是在spring.factories中,在启动时加载进来的

4.springboot会去默认的位置寻找配置文件,默认位置如下

locations.add(ConfigDataLocaiton.of("optional:classpath:/;optional:classpath:/config/"))

locations.add(ConfigDataLocaiton.of("optional:file:./;optional:file:./config/;optional:file:./config/*/"))

外部的配置文件会override内部的配置文件

配置项的优先级如下:

1.Command line arguments

2.ServletConfig init parameters

3.ServletContext init parameters

4.Java System properties(System.getProperties())

5.OS environment variables(System.getEnv())

6.Config data(such as application.properties())

7.@PropertySource annotations on your @Configuration(引入的其它配置文件)

8.Default properties(specified by setting SpringApplication.setDefaultProperties)

5.static 标识的属性不能生效

6.方法参数中也可以用@Value

ContextAnnotationAutowireCandidateResolver的getSuggestedValue(DependencyDescriptor descriptor)方法可以获取这个注解${xxx}

7.系统环境变量中可以匹配到包含.-_的变量

如原变量@Value("${eim.tts.middlePlatform.url}")

环境变量可以设置为eim_tts_middlePlatform_url或者eim-tts-middlePlatform-url或者eim.tts.middlePlatform.url,也不区分大小写

8.想从其它位置获取配置项

If you want to support your own locations,see the ConfigDataLocationResolver and ConfigDataLoader Classes in the org.springframework.boot.context.config package

9.The EnvironmentPostProcessor  interface allows you to manipulate the Environment

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值