Spring PropertyPlaceholderConfigurer Example – Reading a Property File

It’s never a good practice to hard code and combine any environment related details such a username, password or file path etc. along with spring bean configuration. Usually in Java we will be having those details written in a separate property file and it will be read during the execution, the same can be followed in the spring as well using PropertyPlaceholderConfigurer.

Spring comes with a bean factory post processor called PropertyPlaceholderConfigurer which allows you to externalize those details into a properties file. In this Spring PropertyPlaceholderConfigurer Example we will learn how to use the PropertyPlaceholderConfigurer. [Read more…]

Spring Bean Creation – Static Factory Method & Instance Factory Method

In Spring we can create bean using Spring FactoryBean, FactoryBean is an interface and we need to give implementations for the methods in it. If you don’t want to go by that methodology but still want Java Factory Pattern to be implemented then we can go for Static Factory Method and Instance Factory Method.

The client who requests for an object can simply make a call to the factory method which we have defined without knowing about the creation detail. We will be using factory-method and factory-bean attribute in our configuration for the Injection of Bean, through the below spring factory pattern example lets learn more about it. [Read more…]

Spring Bean Life Cycle – Bean Initialization and Destruction

Spring IoC container is also responsible for managing the Spring Bean Life Cycle, the life cycle of beans consist of call back methods such as Post initialization call back method and Pre destruction call back method. Below steps are followed by Spring IoC Container to manage bean life cycle.

Spring Bean Life Cycle

  1. Creation of bean instance by a factory method.
  2. Set the values and bean references to the bean properties.
  3. Call the initialization call back method.
  4. Bean is ready for use.
  5. Call the destruction call back method.

[Read more…]

Spring Autowiring using Annotation – @Autowired, @Resource, @Qualifier, @Inject Annotation

Spring Autowiring by using the “autowire” attribute in the bean configuration file  we can wire all the properties of the bean class. Using Spring Autowiring through XML you cannot wire a particular property. In those cases we can use the Spring @Autowired annotation which allows auto-wiring of setter method, a constructor, a field, or even an arbitrary method.

[Read more…]

Spring Dependency Checking and Spring @Required Annotation

For a large-scale application, there can be hundreds of bean declared in the Spring IoC container and managing dependency between them will be already complicated. Especially through Setter Injection we cannot make sure that values for all the properties has been injected. Spring dependency checking feature will come into rescue in this situation which helps us to check if all/ certain property is injected or not. Spring dependency check can be turned on just by adding “dependency-check” attribute of the <bean> tag. [Read more…]

How to create Spring Beans Using Spring FactoryBean

In Spring you can also Create and Configure of Spring Bean using Spring’s FactoryBean . A factory bean in Spring serves as a factory for creating other beans within the Spring IoC container. In Java terms we can say that, a factory bean is very similar to a factory method (Java Factory Pattern), but here it is Spring specific bean which can be identified by the Spring IoC container during bean construction. [Read more…]

How to Create and Configure Beans in the Spring IoC Container

Spring provides a powerful IoC container to manage the beans which makes up an application. In Spring, Beans can be configured in the Spring IoC container through XML files, properties files, annotations. Spring allows you to configure your beans in one or more bean configuration files. For a simple application we can have a single configuration file whereas in the case of a large application a single file becomes unmanageable and hence we can separate them into multiple configuration file based on their functionality (DAO, Controllers etc.). Lets create a Student Application, through it we will learn how to create and configure beans in Spring IoC Container.

[Read more…]

How to Instantiate Spring IoC Container

In order to get the bean instances from the configuration file, we need to instantiate the Spring IoC Container which in-turn helps us reading the configurations. Spring provides two types of IoC Container implementation.

  1. Bean Factory
  2. Application Context

Bean factory is the more basic implementation of Spring IoC Container.  Application context is the more advanced implementation. Both IoC containers will be having the same Bean configurations only. [Read more…]