首頁 > 軟體

Spring範例講解條件注入方法

2022-06-21 14:10:09

簡介

說明

本文用範例介紹Spring的條件注入的用法。

@Component、@Configuration+@Bean都可以與條件注入的註解結合。

@Component+條件註解

Bean

package com.example.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true")
public class MyComponent {
    public MyComponent() {
        System.out.println("[MyComponent#MyComponent]");
    }
}

application.yml

custom:
  myComponent:
    enabled: true

執行結果:

[MyComponent#MyComponent]

若將application.yml的custom.myComponent.enabled去掉,或者設定為非true值,則不會輸出上邊的執行結果。

@Configuration+@Bean+條件註解

Bean

package com.example.config;
public class MyComponent {
    public MyComponent() {
        System.out.println("[MyComponent#MyComponent]");
    }
}

設定類

package com.example.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {
    @Bean
    @ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true")
    public MyComponent getMyComponent() {
        return new MyComponent();
    }
}

application.yml

custom:
  myComponent:
    enabled: true

執行結果:

[MyComponent#MyComponent]

若將application.yml的custom.myComponent.enabled去掉,或者設定為非true值,則不會輸出上邊的執行結果。

@Configuration+條件註解+@Bean

Bean

package com.example.config;
public class MyComponent {
    public MyComponent() {
        System.out.println("[MyComponent#MyComponent]");
    }
}

設定類

package com.example.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true")
public class MyConfig {
    @Bean
    public MyComponent getMyComponent() {
        return new MyComponent();
    }
}

application.yml

custom:
  myComponent:
    enabled: true

執行結果:

[MyComponent#MyComponent]

若將application.yml的custom.myComponent.enabled去掉,或者設定為非true值,則不會輸出上邊的執行結果。

自定義Condition

自定義的condition的matches方法返回值為true時,才會建立bean。

條件類

//判斷當前系統是否是Mac

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class MyCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, 
                           AnnotatedTypeMetadata annotatedTypeMetadata) {
        return conditionContext.getEnvironment().getProperty("os.name").contains("Mac");
    }
}
@Configuration
public class Config {
    @Conditional(MyCondition.class)
    @Bean
    public String condition() {
        System.err.println("This is mac");
        return "";
    }
}

到此這篇關於Spring範例講解條件注入方法的文章就介紹到這了,更多相關Spring條件注入內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com