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

Spring boot中@Conditional和spring boot的自動(dòng)配置實(shí)例詳解

 更新時(shí)間:2018年05月29日 17:04:43   作者:JAVA開發(fā)老菜鳥  
本文通過實(shí)例給大家介紹了Spring boot中@Conditional和spring boot的自動(dòng)配置,需要的朋友可以參考下

我們知道,spring boot自動(dòng)配置功能可以根據(jù)不同情況來決定spring配置應(yīng)該用哪個(gè),不應(yīng)該用哪個(gè),舉個(gè)例子:

  • Spring的JdbcTemplate是不是在Classpath里面?如果是,并且DataSource也存在,就自動(dòng)配置一個(gè)JdbcTemplate的Bean
  • Thymeleaf是不是在Classpath里面?如果是,則自動(dòng)配置Thymeleaf的模板解析器、視圖解析器、模板引擎

那個(gè)這個(gè)是怎么實(shí)現(xiàn)的呢?原因就在于它利用了Spring的條件化配置,條件化配置允許配置存在于應(yīng)用中,但是在滿足某些特定條件前會(huì)忽略這些配置。

要實(shí)現(xiàn)條件化配置我們要用到@Conditional條件化注解。接下來寫個(gè)小例子來感受下@Conditional是怎么工作的。

一、@Conditional小例子

我們知道在windows下顯示列表的命令是dir,而在linux系統(tǒng)下顯示列表的命令是ls,基于條件配置,我們可以實(shí)現(xiàn)在不同的操作系統(tǒng)下返回不同的值。

1.判斷條件定義

1.)windows下的判定條件

/**
 * 實(shí)現(xiàn)spring 的Condition接口,并且重寫matches()方法,如果操作系統(tǒng)是windows就返回true
 *
 */
public class WindowsCondition implements Condition{
  @Override
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    return context.getEnvironment().getProperty("os.name").contains("Windows");
  }
}

2.)linux下的判定條件

/**
 * 實(shí)現(xiàn)spring 的Condition接口,并且重寫matches()方法,如果操作系統(tǒng)是linux就返回true
 *
 */
public class LinuxCondition implements Condition{
  @Override
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
    return context.getEnvironment().getProperty("os.name").contains("Linux");
  }
}

2.不同系統(tǒng)下的Bean的類

1.)接口

public interface ListService {
  public String showListLine();
}

2.)windows下的Bean類

public class WindowsListService implements ListService{
  @Override
  public String showListLine() {
    return "dir";
  }
}

3.)linux下的Bean的類

public class LinuxListService implements ListService{
  @Override
  public String showListLine() {
    return "ls";
  }
}

3.配置類

@Configuration
public class ConditionConfig {
  /**
   * 通過@Conditional 注解,符合windows條件就返回WindowsListService實(shí)例
   * 
   */
  @Bean
  @Conditional(WindowsCondition.class)
  public ListService windonwsListService() {
    return new WindowsListService();
  }
  /**
   * 通過@Conditional 注解,符合linux條件就返回LinuxListService實(shí)例
   * 
   */
  @Bean
  @Conditional(LinuxCondition.class)
  public ListService linuxListService() {
    return new LinuxListService();
  }
}

4.測(cè)試類

public class ConditionTest {
  public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConditionConfig.class);
    ListService listService = context.getBean(ListService.class);
    System.out
        .println(context.getEnvironment().getProperty("os.name") + " 系統(tǒng)下的列表命令為: " + listService.showListLine());
  }
}

5.運(yùn)行測(cè)試類,由于我的是windows7 系統(tǒng),因此結(jié)果是

Windows 7 系統(tǒng)下的列表命令為: dir

如果你的是linux系統(tǒng),則結(jié)果就會(huì)是

Linux 系統(tǒng)下的列表命令為: ls

二、spring boot 的條件化配置

在spring boot項(xiàng)目中會(huì)存在一個(gè)名為spring-boot-autoconfigure的jar包

 

條件化配置就是在這個(gè)jar里面實(shí)現(xiàn)的,它用到了如下的條件化注解,這些注解都是以@Conditional開頭的:

 

接下來我們看個(gè)源碼的列子:

以JdbcTemplateAutoConfiguration為例,它里面有這段代碼:

@Bean
  @Primary
  @ConditionalOnMissingBean(JdbcOperations.class)
  public JdbcTemplate jdbcTemplate() {
    return new JdbcTemplate(this.dataSource);
  }

只有在不存在JdbcOperations(如果查看JdbcTemplate的源碼,你會(huì)發(fā)現(xiàn)JdbcTemplate類實(shí)現(xiàn)了JdbcOperations接口)實(shí)例的時(shí)候,才會(huì)初始化一個(gè)JdbcTemplate 的Bean。

基于以上內(nèi)容,我們就可以閱讀自動(dòng)配置相關(guān)的源碼了。

總結(jié)

以上所述是小編給大家介紹的Spring boot中@Conditional和spring boot的自動(dòng)配置,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論