深入Spring Boot:那些注入不了的 Spring 占位符 ( ${} 表达式 )
Spring里的占位符
spring里的占位符通常表现的形式是:
|
1
2
3
|
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"><property name="url" value="${jdbc.url}"/></bean> |
或者
|
1
2
3
4
5
6
|
@Configuration@ImportResource("classpath:/com/acme/properties-config.xml")public class AppConfig { @Value("${jdbc.url}") private String url;} |
Spring应用在有时会出现占位符配置没有注入,原因可能是多样的。
本文介绍两种比较复杂的情况。
占位符是在Spring生命周期的什么时候处理的
Spirng在生命周期里关于Bean的处理大概可以分为下面几步:
- 加载Bean定义(从xml或者从@Import等)
- 处理BeanFactoryPostProcessor
- 实例化Bean
- 处理Bean的property注入
- 处理BeanPostProcessor
当然这只是比较理想的状态,实际上因为Spring Context在构造时,也需要创建很多内部的Bean,应用在接口实现里也会做自己的各种逻辑,整个流程会非常复杂。
那么占位符(${}表达式)是在什么时候被处理的?
- 实际上是在org.springframework.context.support.PropertySourcesPlaceholderConfigurer里处理的,它会访问了每一个bean的BeanDefinition,然后做占位符的处理
- PropertySourcesPlaceholderConfigurer实现了BeanFactoryPostProcessor接口
- PropertySourcesPlaceholderConfigurer的 order是Ordered.LOWEST_PRECEDENCE,也就是最低优先级的
结合上面的Spring的生命周期,如果Bean的创建和使用在PropertySourcesPlaceholderConfigurer之前,那么就有可能出现占位符没有被处理的情况。
例子1:Mybatis 的 MapperScannerConfigurer引起的占位符没有处理
例子代码:mybatis-demo.zip
- 首先应用自己在代码里创建了一个DataSource,其中${db.user}是希望从application.properties里注入的。代码在运行时会打印出user的实际值。
|
1
2
3
4
5
6
7
8
9
10
11
|
@Configurationpublic class MyDataSourceConfig { @Bean(name = "dataSource1") public DataSource dataSource1(@Value("${db.user}") String user) { System.err.println("user: " + user); JdbcDataSource ds = new JdbcDataSource(); ds.setURL("jdbc:h2:˜/test"); ds.setUser(user); return ds; }} |
- 然后应用用代码的方式来初始化mybatis相关的配置,依赖上面创建的DataSource对象
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@Configurationpublic class MybatisConfig1 { @Bean(name = "sqlSessionFactory1") public SqlSessionFactory sqlSessionFactory1(DataSource dataSource1) throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); org.apache.ibatis.session.Configuration ibatisConfiguration = new org.apache.ibatis.session.Configuration(); sqlSessionFactoryBean.setConfiguration(ibatisConfiguration); sqlSessionFactoryBean.setDataSource(dataSource1); sqlSessionFactoryBean.setTypeAliasesPackage("sample.mybatis.domain"); return sqlSessionFactoryBean.getObject(); } @Bean MapperScannerConfigurer mapperScannerConfigurer(SqlSessionFactory sqlSessionFactory1) { MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory1"); mapperScannerConfigurer.setBasePackage("sample.mybatis.mapper"); return mapperScannerConfigurer; }} |
当代码运行时,输出结果是:
|
1
|
user: ${db.user} |
为什么会user这个变量没有被注入?
分析下Bean定义,可以发现MapperScannerConfigurer它实现了BeanDefinitionRegistryPostProcessor。这个接口在是Spring扫描Bean定义时会回调的,远早于BeanFactoryPostProcessor。
所以原因是:
- MapperScannerConfigurer它实现了BeanDefinitionRegistryPostProcessor,所以它会Spring的早期会被创建
- 从bean的依赖关系来看,mapperScannerConfigurer依赖了sqlSessionFactory1,sqlSessionFactory1依赖了dataSource1
- MyDataSourceConfig里的dataSource1被提前初始化,没有经过PropertySourcesPlaceholderConfigurer的处理,所以@Value(“${db.user}”) String user 里的占位符没有被处理
要解决这个问题,可以在代码里,显式来处理占位符:
|
1
|
environment.resolvePlaceholders("${db.user}") |
例子2:Spring boot自身实现问题,导致Bean被提前初始化
例子代码:demo.zip
Spring Boot里提供了@ConditionalOnBean,这个方便用户在不同条件下来创建bean。里面提供了判断是否存在bean上有某个注解的功能。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
@Target({ ElementType.TYPE, ElementType.METHOD })@Retention(RetentionPolicy.RUNTIME)@Documented@Conditional(OnBeanCondition.class)public @interface ConditionalOnBean { /** * The annotation type decorating a bean that should be checked. The condition matches * when any of the annotations specified is defined on a bean in the * {@link ApplicationContext}. * @return the class-level annotation types to check */ Class<? extends Annotation>[] annotation() default {}; |
比如用户自己定义了一个Annotation:
|
1
2
3
4
|
@Target({ ElementType.TYPE })@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation {} |
然后用下面的写法来创建abc这个bean,意思是当用户显式使用了@MyAnnotation(比如放在main class上),才会创建这个bean。
|
1
2
3
4
5
6
7
8
9
|
@Configurationpublic class MyAutoConfiguration { @Bean // if comment this line, it will be fine. @ConditionalOnBean(annotation = { MyAnnotation.class }) public String abc() { return "abc"; }} |
这个功能很好,但是在spring boot 1.4.5 版本之前都有问题,会导致FactoryBean提前初始化。
在例子里,通过xml创建了javaVersion这个bean,想获取到java的版本号。这里使用的是spring提供的一个调用static函数创建bean的技巧。
|
1
2
3
4
5
6
7
8
9
10
|
<bean id="sysProps" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass" value="java.lang.System" /> <property name="targetMethod" value="getProperties" /></bean><bean id="javaVersion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="sysProps" /> <property name="targetMethod" value="getProperty" /> <property name="arguments" value="${java.version.key}" /></bean> |
我们在代码里获取到这个javaVersion,然后打印出来:
|
1
2
3
4
5
6
7
8
9
|
@SpringBootApplication@ImportResource("classpath:/demo.xml")public class DemoApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args); System.err.println(context.getBean("javaVersion")); }} |
在实际运行时,发现javaVersion的值是null。
这个其实是spring boot的锅,要搞清楚这个问题,先要看@ConditionalOnBean的实现。
- @ConditionalOnBean实际上是在ConfigurationClassPostProcessor里被处理的,它实现了BeanDefinitionRegistryPostProcessor
- BeanDefinitionRegistryPostProcessor是在spring早期被处理的
- @ConditionalOnBean的具体处理代码在org.springframework.boot.autoconfigure.condition.OnBeanCondition里
- OnBeanCondition在获取bean的Annotation时,调用了beanFactory.getBeanNamesForAnnotation
|
1
2
3
4
5
6
7
8
9
|
private String[] getBeanNamesForAnnotation( ConfigurableListableBeanFactory beanFactory, String type, ClassLoader classLoader, boolean considerHierarchy) throws LinkageError { String[] result = NO_BEANS; try { @SuppressWarnings("unchecked") Class<? extends Annotation> typeClass = (Class<? extends Annotation>) ClassUtils .forName(type, classLoader); result = beanFactory.getBeanNamesForAnnotation(typeClass); |
- beanFactory.getBeanNamesForAnnotation 会导致FactoryBean提前初始化,创建出javaVersion里,传入的${java.version.key}没有被处理,值为null。
- spring boot 1.4.5 修复了这个问题:https://github.com/spring-projects/spring-boot/issues/8269
实现spring boot starter要注意不能导致bean提前初始化
用户在实现spring boot starter时,通常会实现Spring的一些接口,比如BeanFactoryPostProcessor接口,在处理时,要注意不能调用类似beanFactory.getBeansOfType,beanFactory.getBeanNamesForAnnotation 这些函数,因为会导致一些bean提前初始化。
而上面有提到PropertySourcesPlaceholderConfigurer的order是最低优先级的,所以用户自己实现的BeanFactoryPostProcessor接口在被回调时很有可能占位符还没有被处理。
对于用户自己定义的@ConfigurationProperties对象的注入,可以用类似下面的代码:
|
1
2
3
4
|
@ConfigurationProperties(prefix = "spring.my")public class MyProperties { String key;} |
|
1
2
3
4
5
6
7
8
9
10
|
public static MyProperties buildMyProperties(ConfigurableEnvironment environment) { MyProperties myProperties = new MyProperties(); if (environment != null) { MutablePropertySources propertySources = environment.getPropertySources(); new RelaxedDataBinder(myProperties, "spring.my").bind(new PropertySourcesPropertyValues(propertySources)); } return myProperties;} |
总结
- 占位符(${}表达式)是在PropertySourcesPlaceholderConfigurer里处理的,也就是BeanFactoryPostProcessor接口
- spring的生命周期是比较复杂的事情,在实现了一些早期的接口时要小心,不能导致spring bean提前初始化
- 在早期的接口实现里,如果想要处理占位符,可以利用spring自身的api,比如 environment.resolvePlaceholders(“${db.user}”)
一个Java交流平台分享给你们,让你在实践中积累经验掌握原理。如果你想拿高薪,想突破瓶颈,想跟别人竞争能取得优势的,想进BAT但是有担心面试不过的,可以加我的Java学习交流群:642830685
注:加群要求
1、大学学习的是Java相关专业,毕业后面试受挫,找不到对口工作
2、在公司待久了,现在过得很安逸,但跳槽时面试碰壁。需要在短时间内进修、跳槽拿高薪的
3、参加过线下培训后,知识点掌握不够深刻,就业困难,想继续深造
4、已经在Java相关部门上班的在职人员,对自身职业规划不清晰,混日子的
5、有一定的C语言基础,接触过java开发,想转行的
深入Spring Boot:那些注入不了的 Spring 占位符 ( ${} 表达式 )的更多相关文章
- Spring Boot使用Maven打包替换资源文件占位符
在Spring Boot开发中,通过Maven构建项目依赖是一件比较舒心的事,可以为我们省去处理冲突等大部分问题,将更多的精力用于业务功能上.近期在项目中,由于项目集成了其他外部系统资源文件,需要根据 ...
- spring boot 配置注入
spring boot配置注入有变量方式和类方式(参见:<spring boot 自定义配置属性的各种方式>),变量中又要注意静态变量的注入(参见:spring boot 给静态变量注入值 ...
- Spring Boot动态注入删除bean
Spring Boot动态注入删除bean 概述 因为如果采用配置文件或者注解,我们要加入对象的话,还要重启服务,如果我们想要避免这一情况就得采用动态处理bean,包括:动态注入,动态删除. 动态注入 ...
- spring boot 系列之六:深入理解spring boot的自动配置
我们知道,spring boot自动配置功能可以根据不同情况来决定spring配置应该用哪个,不应该用哪个,举个例子: Spring的JdbcTemplate是不是在Classpath里面?如果是,并 ...
- # 曹工说Spring Boot源码(10)-- Spring解析xml文件,到底从中得到了什么(context:annotation-config 解析)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 曹工说Spring Boot源码(12)-- Spring解析xml文件,到底从中得到了什么(context:component-scan完整解析)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 曹工说Spring Boot源码(15)-- Spring从xml文件里到底得到了什么(context:load-time-weaver 完整解析)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 曹工说Spring Boot源码(16)-- Spring从xml文件里到底得到了什么(aop:config完整解析【上】)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 曹工说Spring Boot源码(18)-- Spring AOP源码分析三部曲,终于快讲完了 (aop:config完整解析【下】)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 曹工说Spring Boot源码(29)-- Spring 解决循环依赖为什么使用三级缓存,而不是二级缓存
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
随机推荐
- HashMap如何实现序列化
/** * Save the state of the <tt>HashMap</tt> instance to a stream (i.e., * serialize it) ...
- JS基础_强制类型转换
强制类型转换 将一个数据类型强制转换为其他的数据类型 类型转换主要指,将其他数据类型,转换为 string.number.boolean 1.将其他数据类型转换为string(返回值是强转后类型的值) ...
- LeetCode 61. 旋转链表(Rotate List)
题目描述 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出 ...
- 黑马lavarel教程---6、简单验证
黑马lavarel教程---6.简单验证 一.总结 一句话总结: 1.验证的最简单实例:$request的validate方法,验证通过可以继续进行,验证失败就重定向 2.中文提示可以用中文语言包 3 ...
- Entity Framework Code First使用者的福音 --- EF Power Tool使用记之一
下次会为大家深入解析这个小工具. 最先看到这个工具是在EF产品组最新的博客文章上,http://blogs.msdn.com/b/adonet/archive/2011/05/18/ef-power ...
- SELECT list is not in GROUP BY clause and contains nonaggregated
安装了mysql5.7,用group by 查询时抛出如下异常 SQLSTATE[42000]: Syntax error or access violation: 1055 Expression # ...
- AI换脸必备知识:如何查看显卡型号以及显存大小!
使用Deepfakes(AI换脸) 软件,拼的就是配置,耗的就是时间,考验的是耐心. 配置好了,时间就少了. 所以玩这种软件,硬核需求就是:配置,配置,配置. 我的电脑能跑这个软件么?也是很多新手的 ...
- PHP LDA off 解决
搭建完zabbix初始登录zabbix显示 PHP LDAP off 解决 不需要重新编译php 就可以增加 LDAP 模块 .首先进入自己的 PHP 安装目录中找到 ldap 文件夹 [root@b ...
- 【Java基础】对象序列化与读写
package com.test.io; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...
- AJAX中同步和异步的区别和使用场景
一.简介Ajax请求最重要的问题是代码执行的顺序.最长遇到的问题是,我们定义一个变量接收ajax异步请求的返回结果,后续代码使用,然而后续代码在使用时该变量为初始值,始终得不到想要的结果!!!二.示例 ...