SourcesPlaceholderConfigurer的处理,所以@Value(“${db.user}”) String user 里的占位符没有被处理
要解决这个问题,可以在代码里,显式来处理占位符:
environment.resolvePlaceholders("${db.user}")
例子2:Spring boot自身实现问题,导致Bean被提前初始化
例子代码:demo.zip
Spring Boot里提供了@ConditionalOnBean,这个方便用户在不同条件下来创建bean。里面提供了判断是否存在bean上有某个注解的功能。
@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:
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
然后用下面的写法来创建abc这个bean,意思是当用户显式使用了@MyAnnotation(比如放在main class上),才会创建这个bean。
@Configuration
public 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的技巧。
<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,然后打印出来:
@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
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, classLo