28 Mayıs 2018 Pazartesi

SpringBoot @ConditionalOnClass Anotasyonu

Giriş
Şeklen şöyle

Auto Configuration kapsamında 3 tane seçenek var
1. @ConditionalOnClass
2. @Conditional
3. @ConditionalOnProperty
4. @ConditionalOnMissingBean

Örnek
Şöyle yaparız
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(GreeterService.class)
public class GreeterServiceAutoConfiguration {
  @Bean
  @ConditionalOnMissingBean
  public GreeterService greeterService() {
    return new GreeterService("Welcome", "Mr");
  }
}
Örnek
Şöyle yaparız.
@ConditionalOnClass({DataSource.class, JpaRepository.class})

Hiç yorum yok:

Yorum Gönder