顯示具有 profile 標籤的文章。 顯示所有文章
顯示具有 profile 標籤的文章。 顯示所有文章

2017年12月21日 星期四

Java Spring - Conditional beans


  • Conditional beans
    • 需求:
      • 在某些條件的成立下,此bean才被create,比如某個特定的environment variable被設置以後才create bean
    • 解法:
      • Spring4以後引入一個新的@Conditional annotation -> 若給定的條件成回傳為true,則會create這個bean,否則會被忽略

Java Spring - Configuring profile beans


  • 在開發環境中,遷移是一個挑戰(比如Dev->QA, QA->Prod)
    • 假設有個DB config,在Dev上可能使用的是EmbeddedDatabaseBuilder
    • @Bean(destroyMethod="shutdown")
      public DataSource dataSource() {
          return new EmbeddedDatabaseBuilder()
             .addScript("classpath:schema.sql") //schema會定義在schema.sql中
             .addScript("classpath:test-data.sql")//data使用test-data.sql載入
             .build();
      }