欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot 自動(dòng)配置之條件注解淺析

 更新時(shí)間:2019年02月20日 10:34:21   作者:來醉一場  
這篇文章主要介紹了Spring Boot 自動(dòng)配置之條件注解淺析,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

Spring Boot 神奇的自動(dòng)配置,主要依靠大量的條件注解來使用配置自動(dòng)化。

根據(jù)滿足某一個(gè)特定條件創(chuàng)建一個(gè)特定的Bean。比如說,在某些系統(tǒng)變量下創(chuàng)建Bean,或者只有在某個(gè)Bean創(chuàng)建后才去創(chuàng)建另外一個(gè)Bean. 就是根據(jù)條件來控制Bean的創(chuàng)建行為,可以利用該特性來進(jìn)行一些自動(dòng)配置。

一、常用的條件注解

  • @Conditional 依賴的條件
  • @ConditionalOnBean  在某個(gè)Bean存在的條件下
  • @ConditionalOnMissingBean 在某個(gè)Bean不存在的條件下
  • @ConditionalOnClass  在某個(gè)Class存在的條件下
  • @ConditionalOnMissingClass  在某個(gè)Class不存在的條件下

比較常見的是這些注解,還有其它的比如 @ConditionalOnWebApplication,@ConditionalOnProperty 等,可舉一反三

二、特別說明 @Conditional 注解

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {

 /**
 * All {@link Condition Conditions} that must {@linkplain Condition#matches match}
 * in order for the component to be registered.
 */
 Class<? extends Condition>[] value();

}

使用@Conditional注解,對象需要實(shí)現(xiàn)Condition接口,Condition 接口是一個(gè)函數(shù)式接口

@FunctionalInterface
public interface Condition {

 boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);
}

三、條件注解示例

示例場景:項(xiàng)目中動(dòng)態(tài)的配置Mysql或者Oracle數(shù)據(jù)源

1. 定義配置文件

db-type=oracle

2. 定義Condition類

MySqlCondition.java

public class MySqlCondition implements Condition {

  @Override
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    return "mysql".equals(context.getEnvironment().getProperty("db-type"));
  }
}

OracleCondition.java

public class OracleCondition implements Condition {

  @Override
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    return "oracle".equals(context.getEnvironment().getProperty("db-type"));
  }
}

獲取配置文件db-type的值

3. JdbcFactory接口

public interface JdbcFactory {

  void create();
}

4. 默認(rèn)的Mysql和Oracle實(shí)現(xiàn)

Mysql

@ConditionalOnMissingBean(value = JdbcFactory.class, ignored = MySqlDefaultFactory.class)
@Conditional(MySqlCondition.class)
@Component
public class MySqlDefaultFactory implements JdbcFactory {

  @Override
  public void create() {
    System.out.println("Default MySql create ..");
  }

}

Oracle

@ConditionalOnMissingBean(value = JdbcFactory.class, ignored = OracleDefaultFactory.class)
@Conditional(OracleCondition.class)
@Component
public class OracleDefaultFactory implements JdbcFactory {

  @Override
  public void create() {
    System.out.println("Default oracle create..");
  }
}

5. 測試默認(rèn)實(shí)現(xiàn)方式

@Resource
private JdbcFactory jdbcFactory;

@Test
public void conditionOnMissBean() {
  jdbcFactory.create();
}

結(jié)果:Default MySql create ..

6. 自定義實(shí)現(xiàn)方式

@Component
public class MysqlFactory implements JdbcFactory {

  @Override
  public void create() {
    System.out.println("mysql 。。 create");
  }
}

7. 測試

@Resource
private JdbcFactory jdbcFactory;

@Test
public void conditionOnMissBean() {
  jdbcFactory.create();
}

結(jié)果:mysql 。。 create

8.解析

當(dāng)環(huán)境中不存在 JdbcFactory 的Bean時(shí)則使用默認(rèn)的實(shí)現(xiàn)的方式,如例:沒有自定義實(shí)現(xiàn)時(shí),則默認(rèn)使用MySqlDefaultFactory。這在自動(dòng)化配置中會(huì)經(jīng)常用到。比如redisTemplate 的默認(rèn)實(shí)現(xiàn)

四、GitHub源碼

源碼地址

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論