Skip to main content


Showing posts from April, 2022

React Fragments With Examples

In this tutorial we'll see what is React.Fragment and how to use it. Table of contents Why React Fragment React Fragment Usage Fragment short syntax Key with Fragments Why React Fragment When you write a React component there is a constraint that the JSX expression the component will render should always be wrapped with in a single parent element. Which means trying to write a component as given below results in an error. const HelloWorld = (props) => { return ( <h2>{props.title}</h2> <p>{props.message.greeting} {}</p> ); } The error you get for this component will be something like this- Adjacent JSX elements must be wrapped in an enclosing tag. Did you want a JSX fragment <>...</>? Most frequently <div> is used as a wrapper in such scenarios. const HelloWorld = (props) => { return ( <div> <h2>{props.title}</h2> <p>{

Spring Bean Lifecycle Callback Methods

In Spring framework it is the Spring container that is responsible for instantiating beans, setting bean properties, wiring dependencies and managing the complete bean lifecycle from its instantiation to the time bean is destroyed. Table of contents Spring bean lifecycle callbacks Spring bean lifecycle callback methods Spring bean lifecycle callback methods execution order InitializingBean and DisposableBean callback interfaces InitializingBean and DisposableBean interfaces example Custom init and destroy methods in Spring bean lifecycle @PostConstruct and @PreDestroy annotations in Spring bean lifecycle Spring bean lifecycle callback methods example Aware interfaces in Spring framework Spring Aware interfaces example Spring bean lifecycle callbacks To interact with the container’s management of the bean lifecycle, in order to customize the nature of a bean, Spring Framework provides a number of interfaces which can be categorized into f

Exclude Bean From Autowiring in Spring

If you want to exclude bean from autowiring in Spring on a per-bean basis then in Spring’s XML format it can be done by setting autowire-candidate attribute of the <bean/> element to false. The Spring container makes that specific bean definition unavailable for autowiring (that is applicable to annotation style configurations such as @Autowired too). Excluding bean in autowiring - autowire-candidate example In the example there is a class to place order called OrderService and purchase can be done from a Store. In OrderService class dependency for store has to be autowired. There are two classes of type IStore and you want to exclude one of the bean from autowiring so that NoUniqueBeanDefinitionException is not thrown. public interface OrderService { public void buyItems(); } import org.springframework.beans.factory.annotation.Autowired; public class OrderServiceImpl implements OrderService { private IStore store; @Autowired public OrderServiceImpl(IStore stor

Circular Dependency in Spring

In this post we’ll see what is circular dependency in Spring and how to resolve circular dependencies. Circular dependency in Spring If you mostly use constructor dependency injection , it is possible to create an unresolvable circular dependency scenario. For example; Class A requires an instance of class B through constructor injection, and class B requires an instance of class A through constructor injection. In such configuration where beans for classes A and B are to be injected into each other, the Spring IoC container can’t decide which bean should be created first and throws BeanCurrentlyInCreationException at runtime when it detects this circular reference. Circular dependency Spring example Here is an example showing how circular dependency may occur in your application. import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class ClassA { private ClassB b; @Autowired ClassA(ClassB b){

Spring Component Scanning Example

In Spring XML configuration example we saw how configuration metadata is specified in XML that is used to register beans and wire dependencies. Spring framework also provides an option to automatically discover beans and automatically wire dependencies, in this Spring component scanning example we’ll see how that is done. Table of contents Spring Component scan Automatic discovery of bean and automatic wiring Spring Component scan example Component scanning using <context:component-scan/> element - XML Configuration Using Spring @ComponentScan annotation for enabling component scanning Using filters to customize component scanning Spring Component scan By using component scan Spring framework automatically detects the bean classes by scanning the classpath and registers the bean definition with the container. This eliminates the need to use XML to perform bean registration and keeps the XML to bare minimum (or you can use Java configuration).

Spring Java-based Configuration Example

In Spring XML Configuration Example we saw how configuration metadata is specified in XML that is used to register beans and wire dependencies. Spring framework also provides Java-based configuration to configure the Spring container, in this Spring Java-based configuration example we’ll see how that is done. Spring Java-based configuration Main annotations in Spring’s Java based configuration are @Configuration and @Bean . @Configuration - Annotating a class with @Configuration indicates that this class is used as a source of bean definitions. Furthermore, @Configuration classes let inter-bean dependencies be defined by calling other @Bean methods in the same class. @Bean - The @Bean annotation is used to indicate that a method instantiates, configures, and initializes a new object to be managed by the Spring IoC container. The @Bean annotation plays the same role as the element in the XML configuration. You can use @Bean-annotated methods with any Spring @Component.

Spring XML Configuration Example

In this Spring XML configuration example we’ll see how to create a Spring application where bean declaration and bean dependencies configuration is done using XML configuration. For this Spring XML configuration example Maven build tool is used for managing dependencies. Please refer Create Java Project Using Maven in Eclipse to see how to create a Maven project. Maven dependencies For this example we need spring core and spring context dependencies. Spring Version used is 5.1.8 Release which is defined under properties element in pom.xml. Spring core provides the basic framework classes and classes to interact with other modules. Spring context module provides org.springframework.context.ApplicationContext interface which represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans. <project xmlns="" xmlns:xsi=""

ReentrantLock in Java With Examples

ReentrantLock in Java is part of java.util.concurrent.locks package with in the Java concurrency API added in Java 5 which also has other classes like ConcurrentHashMap , CountDownLatch . ReentrantLock class in Java implements java.util.concurrent.locks.Lock interface and provides many locking operations. Table of contents ReentrantLock in Java Concurrency Why call it a ReentrantLock Java ReentrantLock class constructor ReentrantLock Java example ReentrantLock class methods Drawbacks of the ReentrantLock in Java ReentrantLock in Java Concurrency The traditional way of acquiring a mutual exclusion lock in Java is to use synchronized keyword but ReentrantLock provides more flexibility. ReentrantLock allows a lock to be acquired and released in different scopes and allows multiple locks to be acquired and released in any order. With implementation of the Lock interfaces like ReentrantLock acquiring and releasing of lock is more explicit. It has me