Responsive Ads Here

Tuesday, June 9, 2015

Spring Dependency Injection Basics

Spring Dependency Injection

● A kind of Inversion of Control (IoC)
● “Hollywood Principle”
         – Don't call me, I'll call you
● “Container” resolves (injects) dependencies of components by setting implementation object
(push)
● As opposed to component instantiating or Service Locator pattern where component locates implementation (pull)
● Martin Fowler calls it Dependency Injection.

Benefits of Dependency Injection

● Flexible
        – Avoid adding lookup code in business logic
● Testable
        – No need to depend on external resources or containers for testing
        – Automatic testing (as part of nightly build process)
● Maintainable
        – Allows reuse in different application environments by changing configuration files instead of code
        – Promotes a consistent approach across all applications and teams

Two Dependency Injection Variants

● Constructor dependency Injection
       – Dependencies are provided through the constructors of the component
● Setter dependency injection
       – Dependencies are provided through the JavaBean style setter methods of the component
       – More popular than Constructor dependency injection.

BeanFactory

● BeanFactory object is responsible for managing beans and their dependencies
● Your application interacts with Spring's DI container through BeanFactory interface
       – BeanFactory object has to be created by the application typically in the form of XmlBeanFactory
       – BeanFactory object, when it gets created, read bean configuration file and performs the wiring
       – Once created, the application can access the beans via BeanFactory interface

BeanFactory Implementations

● XmlBeanFactory
      – Convenience extension of DefaultListableBeanFactory that reads bean definitions from an XML document.


Reading XML Configuration File via XmlBeanFactory class

import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.FileSystemResource;  public class XmlConfigWithBeanFactory {      public static void main(String[] args) {           XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));           SomeBeanInterface b = (SomeBeanInterface) factory.getBean(“nameOftheBean”);      } }

Bean Configuration File

● Each bean is defined using <bean> tag under the root of the <beans> tag
● The id attribute is used to give the bean its default name
● The class attribute specifies the type of the bean (class of the bean).


Beans

● The term “bean” is used to refer any component managed by the BeanFactory
● The “beans” are in the form of JavaBeans (in most cases)
          – no arg constructor
          – getter and setter methods for the properties
● Beans are singletons by default
● Properties the beans may be simple values or references to other beans

● Beans can have multiple names

Injection Parameter Types

● Spring supports various kinds of injection parameters
         1.Simple values
         2.Beans in the same factory
         3.Beans in another factory
         4.Collections
         5.Externally defined properties

● You can use these types for both setter or constructor injections


What is Autowiring?

● Spring can autowire dependencies through introspection of the bean classes so that you do not have to explicitly specify the bean properties or constructor arguments.
           – Instead of using <ref>
● Bean properties can be autowired either by property names or matching types.
● Constructor arguments can be autowired by matching types.
● Autowiring can potentially save some typing and reduce clutter. However, you should use it with
caution in real-world projects because it might sacrifices the explicitness


No comments:

Post a Comment