Tuesday 21 August 2018

Spring Introduction

Spring
 
     - It is an open source framework for writing Java applications for both Enterprise and internet application development
 
      - Lightweight framework : you only need to bring onto the class path those jars that you're actually going to leverage.

      - No need of heavy application Server: there is no need to run your Java application on a heavy application server. You don't need web logic or web sphere to run a Spring application because Tom, Cat or Jetty will provide everything that you need.


Inversion Of Control

       - Container maintains your class dependencies.
       - Objects injected at run time, not compile time.
       - An object accepts all of the dependencies for construction instead of constructing them itself. Now in doing this, you actually manage all of the construction of dependency objects in a single point of failure. This reduces the amount of code that you have to write, but it also reduces the replication of code.

Advantages of IOC

     - Reduction of noise in your code :  When dealing with dependency injection, because I'm not copy and pasting that construction code over and over, my code can focus on the business logic and not all of this construction noise.
     - Reduces object coupling : Since an object that I am creating doesn't need to know how to create all of its dependencies, that coupling is dramatically reduced.


Application Context

 The Application Context is the heart of an application leveraging the Spring Framework.

Application Context is a read-only wrapper of  BeanFactory, and all of your run time interactions with the BeanFactory or any beans contained in that factory is through this Application Context. The Application Context provides the metadata for all beans created, and also provides a mechanism for creating beans in the correct order. The Application Context is the Inversion of Control Container and all of your Dependency Injections occur here.

    - Provides all facilities for injection of beans at startup and run time.
    - Most of utilizing Spring is actually configuring the IoC container.
    - Application context handles all singleton beans.
    - A Spring application can have one or more ApplicationContexts
    - Web containers always have multiple
     - Parent context can interact with children, but not the other way around
   
Major Benefits of  Java Configuration
      
      - Native language syntax.
      - Compile time checking of configuration.
      - Easier IDE integration.

- Java configuration stems from a class annotated with @Configuration.
- Beans are created as methods of the class, where the method is annotated   with @Bean
- Constant beans can be defined with @Value


To import multiple configurations into a single config file.

Step1 : Import other config class using @Import annotation . Ex: @Import(DataConfig.class)
Step2 : Autowire the Dependencies using @Autowired annotation
           Ex:
                 @Autowired
private CustomerRepository customerRepository;

@Autowired
private SalesOrderRepository salesOrderRepository;

  Bean definitions of SalesOrderRepository  and CustomerRepository  are defined in DataConfig.java configuration class.


No comments:

Post a Comment