How do you create a custom Auto-Configuration in Spring Boot? स्प्रिंग बूट में कस्टम ऑटो-कॉन्फिगरेशन कैसे बनाएं?
A custom auto-configuration is a @Configuration class registered so Spring Boot picks it up automatically when the library is on the classpath.
It is typically guarded with conditional annotations like @ConditionalOnClass and @ConditionalOnProperty so it only activates when relevant dependencies or properties are present, and its beans are usually paired with @ConditionalOnMissingBean so users can override them. The class is registered by listing its fully qualified name in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports.
@Configuration
@ConditionalOnClass(MyService.class)
public class MyServiceAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public MyService myService() {
return new MyServiceImpl();
}
}एक कस्टम ऑटो-कॉन्फिगरेशन एक @Configuration क्लास होती है जिसे इस तरह रजिस्टर किया जाता है कि लाइब्रेरी क्लासपाथ पर होने पर स्प्रिंग बूट इसे अपने आप उठा ले।
इसे आमतौर पर @ConditionalOnClass और @ConditionalOnProperty जैसे कंडीशनल एनोटेशन से सुरक्षित रखा जाता है, और इसके बीन्स को @ConditionalOnMissingBean के साथ जोड़ा जाता है ताकि यूज़र इन्हें ओवरराइड कर सकें। क्लास को AutoConfiguration.imports फाइल में लिस्ट करके रजिस्टर किया जाता है।
@Configuration
@ConditionalOnClass(MyService.class)
public class MyServiceAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public MyService myService() {
return new MyServiceImpl();
}
}Was this answer clear?