Spring 實戰-第三章-條件化的bean

來源:互聯網
上載者:User

標籤:cond   第三章   nts   override   list   port   loader   runner   屬性   

在使用的時候,某些bean需要在某些特定條件化才能執行個體化,spring中使用的@Condition註解實現這個功能。

1.介面

package main.java.soundsystem;public interface CompactDisc {    void play();}

2.實現

package main.java.soundsystem;import org.springframework.context.annotation.Conditional;import org.springframework.context.annotation.Profile;import org.springframework.stereotype.Component;@Component@Profile("dev")public class SgtPepper implements CompactDisc {    private String title ="Sgt. Pepper‘s Lonely Hearts Club Band";    private String artist="The Beatles";    @Override    public void play() {        System.out.println("Playing "+ title +" by "+artist);    }}

3.增加Condition實作類別,需要實現Condition介面的matches方法,

方法返回true,表示使用這個條件的bean滿足條件可建立,返回false,表示條件不滿足,無法建立

package main.java.soundsystem;import org.springframework.context.annotation.Condition;import org.springframework.context.annotation.ConditionContext;import org.springframework.core.type.AnnotatedTypeMetadata;public class SgtPepperCondition implements Condition {    @Override    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {        return true;    }}

ConditionContext是一個介面,大致如下

public interface ConditionContext {    BeanDefinitionRegistry getRegistry();    ConfigurableListableBeanFactory getBeanFactory();    Environment getEnvironment();    ResourceLoader getResourceLoader();    ClassLoader getClassLoader();}

通過ConditionContext可以做到如下幾點:

  • 藉助getRegistry()返回的BeanDefinitionRegistry檢查bean定義;
  • 藉助getBeanFactroy()返回的ConfigurationListableBeanFactory檢查bean是否存在,甚至探查bean的屬性;
  • 藉助getEnvironment()返回的Environment檢查環境變數是否存在以及它的值是什麼;
  • 讀取並探查getResourceLoader()返回的ResourceLoader所載入的資源;
  • 藉助getClassLoader()返回的ClassLoader載入並檢查類是否存在。

4.配置增加條件

package main.java.soundsystem;import org.springframework.context.annotation.*;@Configuration//@ComponentScanpublic class CDPlayerConfig {    @Bean    @Conditional(SgtPepperCondition.class)    @Profile("dev")    public CompactDisc sgtPeppers() {        return new SgtPepper();    }}

5.測試

package main.java.soundsystem;import static org.junit.Assert.*;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ActiveProfiles;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = {CDPlayerConfig.class})@ActiveProfiles("dev")public class CDPlayerTest {    @Autowired    private CompactDisc cd;    @Test    public void cdShouldNotBeNull() {        assertNotNull(cd);        cd.play();    }}

 

Spring 實戰-第三章-條件化的bean

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.