Posts

Java - How to capitalize the first letter of a String

In this section, we will show you how to capitalize the first letter of a String. In Java, we can use  str.substring( 0 , 1 ).toUpperCase() + str.substring( 1 )  to make the first letter of a String as a capital letter (uppercase letter) String str = "java" ; String cap = str .substring( 0 , 1 ).toUpperCase() + str .substring( 1 ); //cap = "Java" or We can use Apache's common library StringUtils.capitalize(str) API to capitalize first letter of the String. String str = "java" ; String cap = StringUtils . capitalize ( str ); //cap == "Java" 1. str.substring(0,1).toUpperCase() + str.substring(1) A complete Java example to capitalize the first letter of a String. public class Main { public static void main ( String [] args) { System . out .println( capitalize ( "knowledgefactory" )); // Knowledgefactory System . out .println( capitalize ( "java" )); // Java } // with some null an

Spring Boot @ConditionalOnBean Annotation Example

Image
In this section we will learn about  @ConditionalOnBean   Annotation. The  @ConditionalOnBean  annotation let a bean be loaded based on the presence of specific bean inside Spring container. The  @ConditionalOnBean  annotation may be used on any class annotated with  @Component ,  @Service  &  @Repository  or on methods annotated with  @Bean . 1. Using @ConditionalOnBean on @Bean method For example,  @ConditionalOnBean (name = "emailNotificationProvider" ) , when the Bean of name  "emailNotificationProvider"  exists in the container, the bean  emailNotification  will be loaded. @Bean ( "emailNotification" ) @ConditionalOnBean (name = "emailNotificationProvider" ) public EmailNotificationService emailNotificationService () { return new EmailNotificationService(); } 2. Using @ConditionalOnBean on @Service class For example,  @ConditionalOnBean (name =  "smsNotificationProvider" ) , when the Bean of name  "smsNotificationProvi

Spring Boot @ConditionalOnResource Annotation Example

Image
In this section we will learn about  @ConditionalOnResource Annotation. The  @ConditionalOnResource  annotation allows you to enable configuration only when a specific resource is available. We can specify resource's location of classpath as well as system locations. The  @ConditionalOnResource  annotation may be used on any class annotated with @Configuration ,  @Component ,  @Service  &  @Repository  or on methods annotated with  @Bean . 1. Using @ConditionalOnProperty on @Bean method and @Configuration class For example,  The EmailNotificationService class is only loaded if the notification configuration file ( notification.properties ) was found on the classpath. The TwitterNotificationService class is only loaded if the notification and message configuration files ( notification.properties and message.properties ) was found on the  classpath . @Configuration @ConditionalOnResource (resources = { "notification.properties" }) public class AppConfig { @Bean ( &