In this post under Spring Core, I will show with example how to use “@Profile” annotation for “@Configuration” annotated classes. In the real world scenario, whenever we develop an application, we develop with a goal that it should perform according to agreed functional requirements, regardless of whether the application is running in production or testing…… Continue reading Using @Profile with @Configuration classes
Category: Spring Core
Changing scopes to @Component annotated class
In this post under Spring Core, I will show with example how to change the scope of class annotated with “@Component” annotation. By default whenever we annotate a class with “@Component” annotation the scope of the bean is set to “singleton”. We can change this by the help of “@Scope” annotation which is also applied…… Continue reading Changing scopes to @Component annotated class
Wiring together @Bean annotated beans using setter approach
In this post under Spring Core, I will explain with example how to wire two beans annotated with @Bean annotation using setter methods. For our example lets take the below two classes. Bean1 package package12; public class Bean1 { private Bean2 bean2; public Bean1() { } public void setBean2(Bean2 bean2) { this.bean2 = bean2; }…… Continue reading Wiring together @Bean annotated beans using setter approach
Changing scopes of @Bean annotated beans
In this post under Spring Core, I will explain with example how to change scopes of Spring beans annotated with “@Bean” annotation. By default the “@Bean” annotated beans have singleton scope. Which means that whenever a dependee bean asks for a reference to dependent bean and the dependent bean scope is “singleton” then Spring makes…… Continue reading Changing scopes of @Bean annotated beans
Wiring together @Bean annotated beans using constructor approach
In this post under Spring Core, I will explain with example how to wire two beans annotabed with @Bean annotation using constructor. For our example lets take the below two bean classes. Bean1 package package10; public class Bean1 { private Bean2 bean2; public Bean1(Bean2 bean2) { System.out.println(“Hello its Bean1”); this.bean2 = bean2; } } As…… Continue reading Wiring together @Bean annotated beans using constructor approach
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
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
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
Registering init and destroy methods using @Bean annotation
In this post under Spring Core, I will explain with example how to register initialization and destroy methods for a bean. FYI, a bean initialization method is called when the bean is created and destroy method is called before bean is garbage collected. All we need to tell Spring is which are the initialization and…… Continue reading Registering init and destroy methods using @Bean annotation