设为首页 加入收藏

TOP

深入Spring Boot:那些注入不了的 Spring 占位符 ( ${} 表达式 )(二)
2017-12-31 06:06:57 】 浏览:685
Tags:深入 Spring Boot 那些 注入 不了 占位 表达式
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
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇分布式实时日志分析解决方案 ELK .. 下一篇IDEA 代码生成插件 CodeMaker

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目