Skip to main content

Java Spring Framework:


CORE:

BASICS:

        The new technology I had learned is the spring Framework core which is the base of the Spring technology or framework in Java. The main feature of the spring is that it is light weight where the run time is very less. Then the next advantage is the framework use the dependency injection in the object reference in Java where java is a OOPs programming language. The spring framework was found by rod johnson in the year of 2002 and licensed in the apache company in 2003. The other advantages of the Spring is that it can be used to build every application or software which is currently in the market. The framework supports:
  • AOP
  • Spring Boot
  • Spring JDBC
  • Spring MVC
  • Spring EJB
  • Spring Hibernate.

        The diagram from the official website of the Spring is a good representation of the Spring Framework architecture which relies on the Spring Core.
Fig 1: Spring Framework

        As per the SOLID (SRP:

        This is a main concept in solid principle is SRP which is Single Responsibility Principles . This means a class should handle a single thing only. For example User details or Customer Details. This is the core concept a Java Program.


class -- user details + customer details == wrong ;

class -- user details  == good ;

        The other principles are that a class should create a object. This checks the usability of a class. If a class does not create a object then it is a waste thing. But the class which act as parent class can be not used but that is an exception. 


         Then the application running should include the interaction between objects it is the main principle. This thing is called dependency of each object.)
        Principles an object in an application should depend on other objects and also called as dependency. This dependency management is what the core provides us. The Object which is dependent on others is called "dependent" and the class which is dependent on the other class is "dependency".

Advantages of the dependency :

  1. The class which is dependent on a dependency does not know the internals of the dependency class because of strong encapsulation.

Disadvantages:

  1. Tightly Coupled - an class totally dependent on other class so we should edit the total source code.
  2. Can not write the unit testing.
        The alternative for the normal tightly coupled thing is the implements. It implements the dependency in the left hand side of creation of new objects is gone but the right side is still there. But the implementation will be 100% loosely coupled and unit testable. But the main disadvantage is that it will break the encapsulation( dependent will know about the dependency). Then another problem is unnecessary dependency creation between objects. As the image itself shows us, the Core is the base for other frameworks. The core is where we study dependency injection, Boilerplates, xml configuration, Annotation configuration, etc.

        Therefore spring technology comes into the game. It eradicates the problem of encapsulation. The spring tech will give the object so that the developer does not need to supply the object directly in the code. So all the code that is written can be just like a framework but the objects can be created by runtime.
Steps To Implement Core:
  1.  Configuration
  2.  Dependency Injector.
  3. Getting Beans and using it.
Configuration Type:

  • XML Configuration.
  • Java Configuration
  • Annotation.
        This part of Core is where we answer some of the questions like "What objects to create?", "How to create them?", "What dependencies are injected?". The objects or dependencies are injected into the class which are dependent.

        After the configuration is done using injection of one object to another using constructor-args in the mapping of xml file and bean configuration.
Fig.2: Demo Code.

Dependency Injector:

        This configuration will be passed to a dependency injector which can be implemented using two classes called BeanFactory and ApplicationContext from the Spring framework. Here in this class we pass the xml file then the injector will parse the xml then the class will create a container and it is called SpringIOC. Inside the container the base dependency object will be created then the dependent objects are created using the constructor of dependency object. The spring container will hold all the objects which are created by dependency injector. Then if we want an object from the container instead of creating the objects manually we can refer to the container in any class.

        When the container is created the dependency injection will take place between the dependent and dependency object inside the container and it is an automatic process. When we create the beanfactory or ApplicationContext the following will happen:
  • Read the Xml file and validate it
  • If Xml is valid then it creates the in-memory logical memory partition inside JVM
  • Loads the spring bean config file and place metadata in logical memory.
  • The logical memory partition created by beanfactor or ApplicationContext is called IOC container and it returns reference of IOC container.
  • Using the getBean() method of both we can inject the content in the container. 1st the request go to the container and search for the id that we had provided and if there is no definition for that id then it throws exception and exits.
  • If the bean definition found then it reads the corresponding class name and load class into JVM and instantiate the object of the class
IOC Principle - Collaboration of objects and managing the life cycle of object is called IOC

Managing Dependencies:

Dependency lookup: If developer writes the code to manually go to container and get object.

Dependency injection: It will provide the objects automatically.

Note: The objects are stored in the JVM but the references only are in the container.

Spring will use reflections to create the objects inside JVM.

IOC- Inversion of controller- If a class has multiple dependencies then get all the dependencies through one class.

Note: There are many ways to achieve IOC where dependency injection is one type.

Types of Spring Containers:

  • BeanFactory.
  • ApplicationContext.
Types of implementing Dependency Injection:
  • Setter Injection.
  • Constructor Injection

Setter Injection:

When we use a setter in the java to inject the object or create the object from dependency to dependent then it is called Setter Injection.

Constructor Injection:

When we use the constructor to inject an object to the dependent from dependency then this is called Constructor Injection.

Bean Factory Implementation times:
  • When we build a standalone applications (dependency injection)
Application Context Implementation times:
  • Standalone.
  • Web apps.
  • I18n.
  • AOP.
  • Application Events.

Criteria to inject using constructor and setter:

  • If the dependency is mandatory then we should use the Constructor injection(IF A is totally dependent on B)
  • If dependency is not mandatory or optional then we can use the Setter Injection.
  • The dependency is final in the CI and may or may not be final in SI.
  • Then when there is cyclic dependency like (A->B->C->A) then the constructor injection cannot be used. It is possible for SI to have cyclic dependency.

Difficulties of XML configuration:

  • The first draw back is the learning of XML as if we need to work with java we need to learn XML.
  • There is no TypeSafety: if we pass wrong reference it will consider.
  • XML cannot recognize the error during compile time.
  • Readability : XML and JAVA codes are in different places so we should switch between them.
  • Maintenance is the main problem as if too many configurations then it is difficult to maintain.
To Overcome these problems we can use the java configuration or Annotation configuration.

Java Configuration:

  • The syntax is "@Configuration"  in the top of the document called "JavaConfig".
  • In XML bean are there we replace it with "@Bean" in the JavaConfig.
  • For ID we can send the directly the object like "public classname objectname(dependency class object){ return new classname(dependency class object)}
Fig 3. The java configuration file.


Fig 4. The executable file where we use annotation.

Annotation Configuration:

  • Using @component and @Autowired annotation we can reduce the configuration code.
  • In both the java and XML configurations we need to write more amount of code. So instead we have annotation configuration where we don't need to write more code.

 










Comments

Popular posts from this blog

Properties and Primitive Insertion in Spring:

 Property Files and Values: The Properties is a subclass of Hashtable class and it represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string. .properties is a file extension for files mainly used in Java-related technologies to store the configurable parameters of an application The Properties file can be used in Java to externalize the configuration and to store the key-value pairs. The Properties.load() method of Properties class is convenient to load .properties file in the form of key-value pairs. In normal java syntax we will use the util.*. to load the properties, URL,  etc. Then we use the load method to load any file and we can use that to retrieve properties. But in spring we can use only one annotation to load the whole properties file and the Annotation is @PropertySource:(file path). The main important job of the property file is to reduce the har...

Annotations and Scope in Spring:

 Basic Annotations: Component: It is used to create the bean without the configuration files like xml and java configurations The auto wired is used along with it to do the dependency injection Autowired: The Autowired annotation is used for the dependency injection the main types are the: Autowired in constructor Autowired in setter Autowired in the field Note : Using the field injection is not recommended. Fig 1. Autowired in constructor. Fig 2. Autowired in the field. Import: It is used to import other configuration files into the class file.  This will be helpful in the scenario of multiple configuration files are there. Fig 3. Import method usage Primary: The @Primary annotation can be present at the starting of a class where it represent the primary class. For example if we implement two classes using a interface and we gave two classes the @Component annotation the spring will be confused.  So when we add the primary tag on any one of them then that one will be cho...