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".
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:
After the configuration is done using injection of one object to another using constructor-args in the mapping of xml file and bean configuration.
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:
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:
Advantages of the dependency :
- The class which is dependent on a dependency does not know the internals of the dependency class because of strong encapsulation.
Disadvantages:
- Tightly Coupled - an class totally dependent on other class so we should edit the total source code.
- Can not write the unit testing.
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:
- Configuration
- Dependency Injector.
- Getting Beans and using it.
- XML Configuration.
- Java Configuration
- Annotation.
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
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.
- 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. |
- 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
Post a Comment