Spring: Big Picture

What is Spring?

Spring makes it easy to create Java enterprise applications.It is a complimentary to Java EE. Spring is open source. It has a large and active community. Spring 5.0 requires JDK 8+ and provides out-of-the-box support for JDK 9.

Fundation: Spring Framework

The Spring Framework is an application framework and inversion of control container for the Java platform. There are 6 key areas: Core, Web, AOP, Data Access, Integration, Test.

spring overview
Spring Framework

Spring Core

Spring Core is Dependency injection container. It creates and maintains objects and their dependencies.

What is Dependecy Injection?

Dependency injection is a pattern that implements IoC. We create has-a relationship in traditional programming by creating and instance of aclass in another class. Dependency Injection in Spring can be done through constructors, setters or fields. The Spring documentation recommends using constructor-based injection for mandatory dependencies, and setter-based injection for optional ones.

Constructor-Based

In the case of constructor-based dependency injection, the container will invoke a constructor with arguments each representing a dependency we want to set.

Setter-Based

For setter-based dependency injection, the container will call setter method sof our class, after invoking a no-argument constructor or no-argument staticfactory method to instantiate the bean.

Field-Based

In case of Field-Based dependency injection, we can inject the dependencies by markingthem with an @Autowired annotation:

public class Store {
    @Autowired
    private Item item; 
}

Spring Web

Spring Web is a framework for handling web requests. It can be handled via Web MVC or WebFlux.

Spring Web MVC

It uses Java Servlet behind the scenes. Advantage of Spring MVC is high lever API to interact with. It is easier than  usage more productivity.

Spring WebFlux

Spring’s Reactive web programing framework.

  • Web requests are executed asynchronously
  • They don’t wait

Spring AOP

Increases the organization of code by allowing the seperation of cross-cutting concerns.

Prevents duplicated code accross the application

Classifc example is Security

Spring Data Access

It makes easier to develop applications that interact with data

Database transactions are much easier than JDBC 

It provides Exception Translation. For more info: https://www.baeldung.com/spring-dataIntegrityviolationexception

Spring Integration

Spring makes it easy to expose and utilize web services.

Exposing web services

@RestController
public class StudentController {

    @GetMapping("/student/{id}")
    public Student getAccount(@PathVariable int id) {
        return findStudentById(id);
    }
}

Utilize web services with RestTemplate

RestTemplate hides details and handles

  • Opening connection
  • Sending command
  • Handling response
package hello;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.client.RestTemplate;

public class Application {

    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String args[]) {
        RestTemplate restTemplate = new RestTemplate();
        Quote quote = restTemplate.getForObject("http://foo.com/student/1", Student.class);
        log.info(quote.toString());
    }

}

Spring Boot

Spring Boot is build on top of Spring framework. The notable features are 

Auto-configuration

Setting up Auto-configuration is easy (e.g.: @EnableAutoConfiguration)

Standalone

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. The typical process for running Java Web Application is packaging application, choosing and configuring webserver, deploying the application to the web server, and starting the web application server. 

Opinionated

Most Spring Boot applications need very little Spring configuration. https://start.spring.io/ is a great example for opinionated view of the Spring platform and third-party libraries

Other Spring Projects

Spring Data

It makes it easy to use data access technologies, relational and non-relational databases, map-reduce frameworks, and cloud-based data services. This is an umbrella project which contains many subprojects that are specific to a given database. The projects are developed by working together with many of the companies and developers that are behind these exciting technologies.

Spring Cloud

Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. microservices). Spring Cloud takes a very declarative approach, and often you get a lot of fetaures with just a classpath change and/or an annotation. Example application that is a discovery client:

@SpringBootApplication
@EnableDiscoveryClient
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

Spring Security Project

Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements

Is it good fit for you?

Advantages

Well engineered
Stood the test of time. It is not a “yet another framework”
Huge community
Widely used in industry
Very actively developed
Scalable

Disadvantages

Increases the size of the deliverable
Hard to debug
Adds memory overhead


Leave a Reply

Your email address will not be published. Required fields are marked *