Posts

Java - Convert negative number to positive

In this section, we will write a Java program to convert negative number to positive (absolute value ) using  Math.abs() . public class Main { public static void main ( String []args) { //Example 1 int sum = 2 + 2 + 2 + 2 + Math . abs (- 2 ); System . out .println( "Sum (absolute value) : " + sum ); //Example 2 int num1 =- 9 ; int num2 = Math . abs ( num1 ); System . out .println( num2 ); } } Console Output: Total 1 (absolute value) : 10 9 More related topics, Java - Convert comma-separated String to a List and List to a comma-separated String Java - Convert hyphen-separated String to a List and List to a hyphen-separated String Java - Convert colon-separated String to a List and List to a colon-separated String Java - How to get current timestamps Java - How to count duplicated items in a List Java – How to Convert Integer to Long Java - Program to Find Largest Element in an Array Java - How to loop an enum

Spring Boot @ConditionalOnClass Annotation Example

Image
In this section we will learn about @ConditionalOnClass Annotation. The  @ConditionalOnClass  can be used to register a bean only when the given class(es) can be found on the class-path. We can define the fully qualified class name either via a String or via a Class<?> reference. For example, when providing library/framework classes to other developers, this annotation can be used to active certain beans only based on the existing of specific classes, e.g. driver-classes etc. The  @ConditionalOnClass  annotation may be used on any class annotated with @Configuration ,  @Component ,  @Service  &  @Repository  or on methods annotated with  @Bean . 1. Using @ConditionalOnClass on @Bean method To illustrate the use of  @ConditionalOnClass , we will  create two simple service classes, SMSNotificationService class and  EmailNotificationService class. public class EmailNotificationService { public EmailNotificationService () { System . out .println( "Inside EmailN

Spring @Import Annotation Example

Image
In this section we will learn about @Import  Annotation. When we run a Spring application, the packages are scanned. Beans are created from @Component , @Service , @Repository and @Configuration classes. Scanning takes place using the @ComponentScan annotation.  It is contained inside the @SpringBootApplication annotation, with which the main class of the application is annotated. All classes that are in the same package as the class with the @ComponentScan (that is, with the @SpringBootApplication ) as well as in the subpackages of this package are scanned. If you generate a Spring Boot project on the https://start.spring.io  site, you can see: the main class with the annotation with the @SpringBootApplication is in the root package. This means that by default all classes that are below are scanned. That is, the Main Application can be in the com.knf.dev.demo package, but not in the java folder without the package. Correct Structure: The EmailNotificationService and SMSNotifica