Using @DependsOn with @Component annotation

In this post under Spring Core, I will show with example how to use “@DependsOn” annotation to indicate dependencies between two beans marked with “@Component” annotation. For our example we have two beans “Bean1” and “Bean2” and both are annotated with “@Component” annotation. Now to declare that “Bean2” is dependent on “Bean1”, we annotate the…… Continue reading Using @DependsOn with @Component annotation

Giving custom name to @Component annotated beans

In this post under Spring Core, I will show with example how to give custom names to classes annotated with “@Component” annotation. By default when we annotate a class with “@Component” annotation, the name of the bean will be created from the class name, where the first letter of the class name will be lowercase.…… Continue reading Giving custom name to @Component annotated beans

Loading .env file from a specific folder

In previous post under DotEnv, I showed that by default DotEnv checks for “.env” file in the project directory when static “load” method is called. So if your project directory is in “D:\ProgrammingConcepts\JavaSEConcepts\DotEnvConcepts” the “.env” file should be present directly under the “DotEnvConcepts” folder. If present in subfolders under “DotEnvConcepts” it will not load that…… Continue reading Loading .env file from a specific folder

Spring @Component and @ComponentScan annotation example

In this post under Spring Core, I will show with example the purpose and how to use “@Component” and “@ComponentScan” annotation together. Till now in all my previous post, I have been defining a bean using “@Bean” annotation in an “@Configuration” annotated class as shown below package package1;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class Example1 {…… Continue reading Spring @Component and @ComponentScan annotation example

Accessing an environment variable

In this post under DotEnv, I will show with example how to access a particular environment variable. For example I have created “.env” file with below data. Env File MY_ENV_VAR1=some_value1 MY_ENV_VAR2=some_value2 Now to retrieve the value of environment variable “MY_ENV_VAR1” we call the non static “get” method of “DotEnv” class as shown below Main class…… Continue reading Accessing an environment variable

Loading .env files

In this post under DotEnv, I will show with example how to load a .env file. Below is the complete main class Main class 1 package defaultPackage; 2 3 import io.github.cdimascio.dotenv.Dotenv; 4 import io.github.cdimascio.dotenv.DotenvEntry; 5 6 public class Example1 { 7 public static void main(String[] args) { 8 Dotenv dotenv = Dotenv.load(); 9 for(DotenvEntry entry…… Continue reading Loading .env files

Using @DependsOn annotation

In this post under Spring Core, I will explain with example the purpose and how to use the “@DependsOn” annotation. In an Object Oriented Programming, no object is an island, it depends on other objects to perform their job. When Spring loads the application context, it creates the beans in following order. If a bean…… Continue reading Using @DependsOn annotation

Using @AllArgsConstructor annotation example

In this post under Lombok, I will explain the purpose of “@AllArgsConstructor” annotation and how to use it. This annotation when added at class level, automatically creates a constructor with all the fields present in the class. If a field is annotated with “@NonNull” annotation, the automatically generated constructor code will add a null check…… Continue reading Using @AllArgsConstructor annotation example