Spring Framework…
Spring Framework is an open-source framework for building web applications with Java as a programming language. It is a powerful lightweight application development framework used for Java Enterprise Edition (JEE). In a way, it is a framework of frameworks because it provides support to various frameworks such as Struts, Hibernate, Tapestry, EJB, JSF, etc.
Features of Spring Framework
The features of the Spring framework such as IoC, AOP, and transaction management, make it unique among the list of frameworks. Some of the most important features of the Spring framework are as follows:
- Inversion Of Control (IoC): In Spring Framework, loose coupling is achieved using Inversion of Control. The objects give their own dependencies instead of creating or looking for dependent objects.
- Data access framework: Allows the developers to use persistence APIs, such as JDBC and Hibernate, for storing persistence data in the database. It helps in solving various problems of the developer, such as how to interact with a database connection, how to make sure that the connection is closed, how to deal with exceptions, and how to implement transaction management.
- Spring MVC framework: Spring Framework is an MVC web application framework. This framework is configurable via interfaces and accommodates multiple view technologies.
- Transaction Management: For transaction management, the Spring framework provides a generic abstraction layer. It is not tied to J2EE environments and it can be used in container-less environments. Helps in handling transaction management of an application without affecting its code. This framework provides Java Transaction API (JTA) for global transactions managed by an application server and local transactions managed by using the JDBC Hibernate, Java Data Objects (JDO), or other data access APIs.
- Aspect-Oriented Programming (AOP): By separating application business logic from system services, Spring Framework supports Aspect-Oriented Programming and enables cohesive development.
- JDBC Exception Handling: The JDBC abstraction layer of the Spring Framework offers an exception hierarchy, which simplifies the error handling strategy.
Spring Framework Architecture
The spring framework is a layered architecture that consists of several modules. all modules are built on top of its core container. its modular architecture enables integration with other frameworks without much hassle.
Spring Core: This module provides the dependency injection (di) feature which is the basic concept of the spring framework. this module contains the bean factory, an implementation of factory pattern which creates the bean as per the configurations provided by the developer in an XML file.
Spring Bean: This module provides an implementation for the factory design pattern through BeanFactory.
Spring Context: This module is built on the solid base provided by the Core and the Beans modules and is a medium to access any object defined and configured.
Spring Expression Languages (SpEL): This module offers expression language for modifying and querying object graph during the runtime.
AOP module: the aspect-oriented programming module allows developers to define method-interceptors and pointcuts to keep the concerns apart. it is configured at run time so the compilation step is skipped. it aims at declarative transaction management which is easier to maintain.
DAO module: this provides an abstraction layer to the low-level task of creating a connection, releasing it, etc. it also maintains a hierarchy of meaningful exceptions rather than throwing complicated error codes from specific database vendors. it uses AOP to manage transactions. transactions can also be managed programmatically.
web module: spring comes with an MVC framework that eases the task of developing web applications. it also integrates well with the most popular MVC frameworks like struts, JSF, etc.
Spring Dependency Injection
Dependency Injection is the main functionality provided by Spring IOC(Inversion of Control). Through which the Spring container “injects” objects into other objects or “dependencies”. The Spring-Core module is responsible for injecting dependencies through either Constructor or Setter methods.
Simply put, this allows for loose coupling of components and moves the responsibility of managing components onto the container.
Need for Dependency Injection:
Suppose class One needs the object of class Two to instantiate or operate a method, then class One is said to be dependent on class Two. Now though it might appear okay to depend on a module on the other, in the real world, this could lead to a lot of problems, including system failure. Hence such dependencies need to be avoided.
Spring IOC resolves such dependencies with Dependency Injection, which makes the code easier to test and reuse. Loose coupling between classes can be possible by defining interfaces for common functionality and the injector will instantiate the objects of required implementation.
Constructor-Based Dependency Injection
In This, the container will invoke a constructor with arguments each representing a dependency we want to set.
Spring resolves each argument primarily by type, followed by name of the attribute and index for disambiguation. Let’s see the configuration of a bean and its dependencies using annotations:
create the configuration of the beans through XML configuration:
The @Configuration annotation indicates that the class is a source of bean definitions.
The @Bean annotation is used on a method to define a bean. If we don’t specify a custom name
For a bean with the default singleton scope, Spring first checks if an instance of the bean already exists and only creates a new one if it doesn’t. If we’re using the prototype scope, the container returns a new bean instance for each method call.
Another way to create the configuration of the beans is through XML configuration:
Setter-Based Dependency Injection
For setter-based Dependency Injection, the container will call setter methods of our class, after invoking a no-argument constructor or no-argument static factory method to instantiate the bean. Let’s create this configuration using annotations:
XML for the same configuration of beans:
Field-Based Dependency Injection
In the case of Field-Based Dependency Injection, we can inject the dependencies by marking them with an @Autowired annotation:
While constructing the Store object, if there’s no constructor or setter method to inject the Item bean, the container will use reflection to inject Item into Store.
Autowiring Dependencies
It allows the Spring container to automatically resolve dependencies between collaborating beans by inspecting the beans that have been defined.
There are four modes of auto wiring a bean using an XML configuration:
- no: the default value — this means no auto wiring is used for the bean and we have to explicitly name the dependencies
- byName: auto wiring is done based on the name of the property, therefore Spring will look for a bean with the same name as the property that needs to be set
- byType: similar to the byName auto wiring, only based on the type of the property.
- constructor: auto wiring is done based on constructor arguments, meaning Spring will look for beans with the same type as the constructor arguments.
For example:
We can also inject beans using the @Autowired annotation for auto wiring by type:
If there’s more than one bean of the same type, we can use the @Qualifier annotation to reference a bean by name:
Conclusion
In this article, I discuss the details about the Spring Framework, Features of Spring Framework, Architecture, and Dependency Injections. That’s it for now.