Spring Framework

Spring Projects

Spring Project 1

Method Injection in Spring Framework
Previous Home Next

Introduction: Spring 1.1 introducing method injection for a new IOC-Oriented feature with allows greater flexibility for interactions between collaborators.

Type of Method Injection

The spring framework method injection provide this facility in to two way:

  1. Lookup Method Injection
  2. Method replacement

Lookup Method Injection: The lookup method injection provide the facility to implement in our application one bean is depends on another bean with a different life cycle. It means that singleton bean depends on non-singleton bean. In this situation setter and constructor injection create instance of singleton bean not a non-singleton bean. In some case application require singleton bean obtain new instance of the non-singleton every time it requires the bean.

If we achieve this implementation in our application the implement BeanFactoryAware interface in our singleton bean. Then using BeanFactory instance, the singleton bean can lookup a new instance of a non-singleton dependency every time it needs it.

The Lookup Method Injection allows the singleton bean to declare that it requires a non-singleton dependency and receive a new instance of the non-singleton bean each time it needs to interact with it, without needing to implement any spring-specific interfaces.

Method Replacement: This Method injection provide a facility to replace the implementation of any method on a bean arbitrary, without changing our source code.

Note: If we are use lookup method injection and method replacement in our application, the add the CGLIB jar file in our application.

Previous Home Next