Spring Core

  • 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

  1. Spring Bean
  2. SpEL (Spring Expression Language)
  3. Spring Context
  1. ORM (Object Relational Mapping)
  2. OXM (Object XML Mappers)
  3. JMS (Java Messaging Service)
  4. Transaction
  1. Web — MVC
  2. Web — Socket
  3. Web — Portlet
  • 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:

  • Aspects — This module provides support to the integration with AspectJ.

A Spring application generally consists of the following components:

  • 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. 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 :

  • 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:

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

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

</bean>

<beans>

<context:annotation-config/>

<!-- bean definitions go here -->

</beans>

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. 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.
  2. 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.
  3. constructor: It injects the dependency by calling the constructor of the class. It has a large number of parameters.
  4. 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:

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store