Spring Core

Abhinav Kumar gupta
7 min readDec 13, 2020

--

  • Spring is a powerful open-source, application framework created to reduce the complexity of enterprise application development.
  • It is light-weighted and loosely coupled.
  • It has layered architecture, which allows you to select the components to use, while also providing a cohesive framework for J2EE application development.
  • Spring framework is also called the framework of frameworks as it provides support to various other frameworks such as Struts, Hibernate, Tapestry, EJB, JSF, etc.

Advantages

  • Because of Spring Frameworks layered architecture, you can use what you need and leave what you don’t.
  • Spring Framework enables POJO (Plain Old Java Object) Programming which in turn enables continuous integration and testability.
  • JDBC is simplified due to Dependency Injection and Inversion of Control.
  • It is open-source and has no vendor lock-in.

Spring Modules

  • Spring Core Container — This layer is basically the core of Spring Framework. It contains the following modules :
  1. Spring Core
  2. Spring Bean
  3. SpEL (Spring Expression Language)
  4. Spring Context
  • Data Access/Integration — This layer provides support to interact with the database. It contains the following modules :
  1. JDBC (Java DataBase Connectivity)
  2. ORM (Object Relational Mapping)
  3. OXM (Object XML Mappers)
  4. JMS (Java Messaging Service)
  5. Transaction
  • Web — This layer provides support to create web applications. It contains the following modules :
  1. Web
  2. Web — MVC
  3. Web — Socket
  4. Web — Portlet
  • Aspect-Oriented Programming (AOP) — In this layer you can use Advice, Pointcuts, etc., to decouple the code.
  • Instrumentation — This layer provides support to class instrumentation and classloader implementations.
  • Test — This layer provides support to testing with JUnit and TestNG.

Few Miscellaneous modules are given below:

  • Messaging — This module provides support for STOMP. It also supports an annotation programming model that is used for routing and processing STOMP messages from WebSocket clients.
  • Aspects — This module provides support to the integration with AspectJ.

A Spring application generally consists of the following components:

  • Interface: It defines the functions.
  • Bean class: It contains properties, its setter and getter methods, functions, etc.
  • Spring Aspect-Oriented Programming (AOP): Provides the functionality of cross-cutting concerns.
  • Bean Configuration File: Contains the information of classes and how to configure them.
  • User program: It uses the function.

IOC Container

At the core of the Spring Framework, lies the Spring container. The container creates the object, wires them together, configures them, and manages their complete life cycle. The Spring container makes use of Dependency Injection to manage the components that make up an application. The container receives instructions for which objects to instantiate, configure, and assemble by reading the configuration metadata provided. This metadata can be provided either by XML, Java annotations, or Java code.

Types of IOC Container

  1. BeanFactory: BeanFactory is like a factory class that contains a collection of beans. It instantiates the bean whenever asked for by clients.
  2. ApplicationContext: The ApplicationContext interface is built on top of the BeanFactory interface. It provides some extra functionality on top BeanFactory.

Dependency Injection

In Dependency Injection, you do not have to create your objects but have to describe how they should be created. You don’t connect your components and services together in the code directly, but describe which services are needed by which components in the configuration file. The IoC container will wire them up together.

In general, dependency injection can be done in three ways, namely :

  • Constructor Injection
  • Setter Injection
  • Interface Injection

In Spring Framework, only constructor and setter injections are used.

Spring Beans

  • They are the objects that form the backbone of the user’s application.
  • Beans are managed by the Spring IoC container.
  • They are instantiated, configured, wired, and managed by a Spring IoC container
  • Beans are created with the configuration metadata that the users supply to the container.

Configuration metadata can be provided to Spring container in the following ways:

  • XML-Based Configuration: In Spring Framework, the dependencies and the services needed by beans are specified in configuration files which are in XML format. These configuration files usually contain a lot of bean definitions and application-specific configuration options. They generally start with a bean tag. For example:

<bean id="studentbean" class="org.Demo.firstSpring.StudentBean">

<property name="name" value="Abhinav"></property>

</bean>

  • Annotation-Based configuration: Instead of using XML to describe a bean wiring, you can configure the bean into the component class itself by using annotations on the relevant class, method, or field declaration. By default, annotation wiring is not turned on in the Spring container. So, you need to enable it in your Spring configuration file before using it. For example:

<beans>

<context:annotation-config/>

<!-- bean definitions go here -->

</beans>

  • Java-based configuration: The key features in Spring Framework’s new Java-configuration support are @Configuration annotated classes and @Bean annotated methods.

1. @Bean annotation plays the same role as the <bean/> element.

2.@Configuration classes allow defining inter-bean dependencies by simply calling other @Bean methods in the same class.

For example:

@Configuration

public class StudentConfig

{

@Bean

public StudentBean myStudent()

{ return new StudentBean(); }

}

AutoWiring

The Spring container is able to autowire relationships between the collaborating beans. That is, it is possible to let Spring resolve collaborators for your bean automatically by inspecting the contents of the BeanFactory.
Different modes of bean auto-wiring are:

  1. no: This is the default setting which means no autowiring. Explicit bean reference should be used for wiring.
  2. byName: It injects the object dependency according to the name of the bean. It matches and wires its properties with the beans defined by the same names in the XML file.
  3. byType: It injects the object dependency according to type. It matches and wires a property if its type matches with exactly one of the names of the bean in the XML file.
  4. constructor: It injects the dependency by calling the constructor of the class. It has a large number of parameters.
  5. autodetect: First the container tries to wire using autowire by the constructor, if it can’t then it tries to autowire by byType.

Spring Annotations

Instead of using XML to describe a bean wiring, the developer moves the configuration into the component class itself by using annotations on the relevant class, method, or field declaration. It acts as an alternative to XML setups. For example:

@Configuration

public class AnnotationConfig

{

@Bean

public MyDemo myDemo()

{ return new MyDemoImpll(); }

}

By default, Annotation wiring is not turned on in the Spring container. Thus, to use annotation-based wiring we must enable it in our Spring configuration file by configuring <context:annotation-config/> element. For example:

<beans xmlns="<a href="http://www.springframework.org/schema/beans">http://www.springframework.org/schema/beans</a>" xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>" xmlns:context="<a href="http://www.springframework.org/schema/context">http://www.springframework.org/schema/context</a>">

<context:annotation-config/>

<beans ………… />

</beans>

@Component: This marks a java class as a bean. It is a generic stereotype for any Spring-managed component. The component-scanning mechanism of spring now can pick it up and pull it into the application context.

@Controller: This marks a class as a Spring Web MVC controller. Beans marked with it are automatically imported into the Dependency Injection container.

@Service: This annotation is a specialization of the Component annotation. It doesn’t provide any additional behavior over the @Component annotation. You can use @Service over @Component in service-layer classes as it specifies intent in a better way.

@Repository: This annotation is a specialization of the @Component annotation with similar use and functionality. It provides additional benefits specifically for DAOs. It imports the DAOs into the DI container and makes the unchecked exceptions eligible for translation into Spring DataAccessException.

@Required: This annotation is applied to bean property setter methods. This annotation simply indicates that the affected bean property must be populated at the configuration time with the help of an explicit property value in a bean definition or with auto wiring. If the affected bean property has not been populated, the container will throw BeanInitializationException.

For example:

public class Employee

{

private String name;

@Required

public void setName(String name)

{this.name=name; }

public string getName()

{ return name; }

}

@Autowired: The @Autowired annotation provides more accurate control over where and how autowiring should be done. This annotation is used to autowire bean on the setter methods, constructor, a property, or methods with arbitrary names or multiple arguments. By default, it is a type-driven injection. For Example:

public class Employee

{

private String name;

@Autowired

public void setName(String name)

{this.name=name; }

public string getName()

{ return name; }

}

@Qualifier: When you create more than one bean of the same type and want to wire only one of them with a property you can use the @Qualifier annotation along with @Autowired to remove the ambiguity by specifying which exact bean should be wired. For example, here we have two classes, Employee and EmpAccount respectively. In EmpAccount, using @Qualifier it's specified that bean with id emp1 must be wired.

Employee.java

public class Employee

{

private String name;

@Autowired

public void setName(String name)

{ this.name=name; }

public string getName()

{ return name; }

}

EmpAccount.java

public class EmpAccount

{

private Employee emp;

@Autowired

@Qualifier(emp1)

public void showName()

{

System.out.println(“Employee name : ”+emp.getName);

}

}

@RequestMapping annotation: @RequestMapping annotation is used for mapping a particular HTTP request method to a specific class/ method in the controller that will be handling the respective request. This annotation can be applied at both levels:

  • Class level: Maps the URL of the request.
  • Method level: Maps the URL as well as the HTTP request method.

--

--

No responses yet