Listening to ApplicationContext events

In this post under Spring Core, I will explain with example how to listen to ApplicationContext events. The Spring framework when creates/stops/refresh an ApplicationContext, it generates certain events and we as a developer can listen to those events and add our custom logic so that it can be executed. For our example, I will create…… Continue reading Listening to ApplicationContext events

Using filters to customize @ComponentScan annotation

In one of my previous posts under Spring Core, I explained the purpose of “@ComponentScan” annotation with example. For recap, “@ComponentScan” annotation when applied at the class level with an package name as an argument tells Spring framework to scan for class files annotated with “@Component” annotationunder the specified package folder. It scans the subfolders…… Continue reading Using filters to customize @ComponentScan annotation

Autowiring interface with multiple implementation

Autowiring interface with multiple implementation In this post, I will explain with example how to inject a bean whose type is an interface (Java Interface or Abstract class) and it has multiple classes that implements it. In the previous post, I showed how Spring Framework inject a bean whose type is an interface and has…… Continue reading Autowiring interface with multiple implementation

Autowiring interface with single implementation

In all my previous posts under Spring Core, I showed how to inject a bean belonging to a particular class. But in reality, to maintain a loose coupling we program to an interface and not to a class. (Interface doesn’t represent Java Interface. Here Interface is generic term that represents either Java Interfaceor Java Abstract…… Continue reading Autowiring interface with single implementation

Autowiring By Name (@Qualifier annotation)

In this post under Spring Core, I will explain with example how to autowire beans based on names instead of bean type. Till now in all our previous posts, beans to be injected were figured out by the class type. Sometimes there exist a situation where when Spring framework tries autowire a bean of a…… Continue reading Autowiring By Name (@Qualifier annotation)

Spring @Primary annotation

In this post under Spring Core, I will explain with example the purpose of “@Primary” annotation. When doing autowiring by type, there may be scenarios where two bean definitions can be considered for injection. For example if we have below DAO class package package26; public class Dao { private String daoName; public Dao(String daoName) {…… Continue reading Spring @Primary annotation

Autowiring Optional beans

In this post under Spring Core, I will show with example how to use Java’s Optional class to declare a bean (to be injected) as optional. In one of my previous post under Spring Core, I showed you about “required” attribute in “@Autowired” annotation. For recap, “@Autowired” annotation has only one attribute named “required” whose…… Continue reading Autowiring Optional beans

Autowiring a collection and array of beans

In this post under Spring Core, I will explain with example how to autowire a collection and array of beans. For our example I will use the below “Dao” class Dao class package package22; public class Dao { private String daoName; public Dao(String daoName) { this.daoName = daoName; } @Override public String toString() { return…… Continue reading Autowiring a collection and array of beans