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 some clean up process just before a bean gets destroyed.

Continue

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 bean.

Continue

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 the Spring container respectively.

Continue

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 implementation for the annotations.

Continue

Spring @ConditionalOnWebApplication and @ConditionalOnNotWebApplication Examples

Introduction 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.

Continue

Spring @ConditionalOnExpression Example

Introduction We will create examples on Spring @ConditionalOnExpression. The @ConditionalOnExpression annotation lets configuration be included based on the result of a SpEL (Spring Expression Language) expression. For our example the Module class is only loaded if a particular SpEL is enabled. This way, we might create similar modules that are only loaded if their respective SpEL has been found or enabled.

Continue

Spring @ConditionalOnResource Example

Introduction We will create examples on Spring @ConditionalOnResource. The @ConditionalOnResource annotation lets configuration be included only when a specific resource is present in the classpath. For our example the Log4j class is only loaded if the log4j configuration file was found on the classpath. This way, we might create similar modules that are only loaded if their respective configuration file has been found.

Continue

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 to specific interfaces. Spring supports Setter Injection via Java bean setters; and Constructor Injection via constructor arguments. You may find useful tutorial on what is dependency pattern?

Continue

Spring @ConditionalOnProperty Example

Introduction We will create examples on Spring @ConditionalOnProperty. The @ConditionalOnProperty annotation allows to load beans conditionally depending on a certain environment property or configuration of a property. Use the prefix and name attributes to specify the property that should be checked. By default, any property that exists and is not equal to false is matched. You can also create more advanced checks by using the havingValue and matchIfMissing attributes.

Continue

Spring @ConditionalOnClass and @ConditionalOnMissingClass

Introduction Spring @ConditionalOnClass and @ConditionalOnMissingClass annotations let @Configuration classes be included based on the presence or absence of specific classes. So @ConditionalOnClass loads a bean only if a certain class is on the classpath and @ConditionalOnMissingClass loads a bean only if a certain class is not on the classpath. This mechanism does not apply the same way to @Bean methods where typically the return type is the target of the condition: before the condition on the method applies, the JVM will have loaded the class and potentially processed method references…

Continue