Spring Boot application.properties Inheritance/Override

Table of Contents Introduction Prerequisites Project Setup Inheritance/Override Testing The Application Source Code Introduction In this example I am going to show you how to inherit values from application.properties into different environments. So, if you have an application.properties file under class path folder src/main/resources and if you have different environment specific application-{env}.properties files, then you csan inherit key/value pairs from application.properties file. Even you can override the inherited key/value…

Inject Prototype Bean into Singleton Bean in Spring – Lookup Method Injection

Lookup Method In this tutorial I am going to show you how lookup method injection works in Spring framework. You may face a situation where you need to inject prototype scoped bean into singleton scoped bean. For singleton scoped bean a new instance or object is created and the same is returned each time it is injected or looked up….

Spring bean life cycle call back Methods Example

Spring framework provides bean life cycle call back methods to perform some additional tasks which you may want to perform when a bean is initiated or created or when a bean is about to get destroyed. Therefore you can run a method which will do some initialization process during bean initialization and you can run another method which will do…

InitializingBean and DisposableBean in Spring

Spring InitializingBean and DisposableBean are the interfaces used in life cycle of Spring beans management. Each of these interfaces has only one method. InitializingBean has a method called afterPropertiesSet() and DisposableBean has a method called destroy(). Spring container runs the method afterPropertiesSet() once the bean properties have been set and Spring container runs destroy() method once Spring container releases the…

Custom init() and destroy() methods in Spring

Introduction In this tutorial we will discuss on Spring custom init() and destroy() methods. These methods are call back methods which used in Spring life cycle. You can use these methods to to some initialization and clean up jobs just after the bean is created by the Spring container or just before the bean is about to be destroyed by…

BeanPostProcessor in Spring

BeanPostProcessor in Spring is used for extending the functionality of framework if want to do any configuration Pre- and Post- bean initialization done by spring container. By default, Spring will not aware of the @PostConstruct and @PreDestroy annotation. To enable it, you have to either register CommonAnnotationBeanPostProcessor or specify <context:annotation-config/> in the bean configuration file. Here CommonAnnotationBeanPostProcessor is predefined BeanPostProcessor…

Spring @ConditionalOnWebApplication and @ConditionalOnNotWebApplication Examples

Condition On Web And Not Web The Spring @ConditionalOnWebApplication and @ConditionalOnNotWebApplication annotations let configuration be included depending on whether the application is a web application. A web application is any application that uses a Spring WebApplicationContext, defines a session scope, or has a StandardServletEnvironment.

Spring @ConditionalOnExpression Example

Condition On Expression I will create examples how to work with Spring Conditional on Expression using @ConditionalOnExpression. The @ConditionalOnExpression annotation lets configuration be included based on the result of a SpEL (Spring Expression Language) expression. For this example the Module class is only loaded if a particular SpEL is enabled. This way, you might create similar modules that are only loaded if their…

Spring @ConditionalOnResource Example

Condition On Resource In this tutorial I will create examples on Spring @ConditionalOnResource. The @ConditionalOnResource annotation lets configuration be included only when a specific resource is present in the classpath. For example, the Log4j class is only loaded if the log4j configuration file (log4j.properties) was found on the class-path. This way, you might create similar modules that are only loaded if their respective…

Dependency injection in Spring

Introduction Here we will see different types of dependency injections in Spring framework. There are three types of dependency injections and they are constructor injection, setter injection and interface injection. Spring supports only constructor and setter injection. Interface injection is supported by other language like Avalon. Interface injection a different type of DI(dependency injection) that involves mapping items to inject…