Skip to content
thesarfo

Reference

Interview Prep: Spring Framework Basics

What Spring is, Inversion of Control, dependency injection, POJOs vs Java Beans, and the IoC container.

views 0
  1. What is Spring Framework? Spring Framework is an open source application framework. It’s a lightweight Inversion of Control (IoC) container and aspect-oriented container framework for the Java platform. Spring handles the infrastructure so we can focus on application development. It was created by Rod Johnson; Spring came into existence in 2003.

  2. What is an application framework? A software library that provides a fundamental structure to support the development of applications for a specific environment — it acts as skeletal support to build an application, meant to lessen the general issues faced during development.

  3. How is Spring lightweight? Compared to traditional Java EE applications, which need a Java EE application server (Glassfish, WildFly, WebLogic, WebSphere) to run — big, complex pieces of software that aren’t trivial to install or configure — Spring doesn’t need any of that. Additionally, Spring provides various modules for different purposes (Core Container, Data Access/Integration, Web, AOP, Instrumentation, Messaging, Test), and you don’t need to inject all of them to use one. For example, you can use Spring JDBC without Spring Web.

  4. What is Inversion of Control (IoC)? A principle in software engineering by which control of objects or portions of a program is transferred to a container or framework. For example, say our application has a text editor component and we want spell checking:

    public class TextEditor {
    private SpellChecker checker = new SpellChecker();
    }

    Here TextEditor needs a SpellChecker object — it’s dependent on it, and we’re manually instantiating it. We’re managing the dependency; we have the control. Now:

    public class TextEditor {
    private SpellChecker checker;
    public TextEditor(SpellChecker checker) {
    this.checker = checker;
    }
    }

    Here we’re asking Spring to instantiate the SpellChecker object and pass it into the TextEditor constructor (Constructor Injection) — Spring is managing the dependency. Control has transferred from programmer to Spring. This is Inversion of Control.

  5. What is an aspect-oriented container framework? Aspect-oriented programming (AOP) is a paradigm that increases modularity by separating cross-cutting concerns. For example, in an app for medical records, indexing records is a core concern, while logging a history of changes, or an authentication system, are cross-cutting concerns.

    • Core concern: logic without which the application wouldn’t exist — e.g., business logic, fetching data from a database/external API.
    • Cross-cutting concern: logic common across the application whose presence/absence doesn’t impact core business functionality, but affects multiple points of the app if applied — e.g., logging, caching, transaction processing, security.
  6. What are the features of Spring Framework?

    • Versatile — a “framework of frameworks,” supporting Struts, Hibernate, Tapestry, EJB, JSF, etc. You can add Spring to an existing project without removing existing technology or rewriting existing functionality.

    • Non-invasive — you don’t need to extend/implement any Spring-provided class/interface in your own classes, so you can swap Spring for another framework without changing your class logic.

    • Lightweight — divided into modules where no module depends on others except Spring Core, so you can learn/use just the part you need (e.g., Spring ORM without Spring Web).

    • Dependency Injection — injecting a target object into a dependent object. E.g., an Employee class depends on an Address object:

      @Component
      class Employee {
      private int id;
      private String name;
      private Address address;
      Employee() {
      id = 10;
      name="David";
      address = new Address();
      }
      }
      @Component
      class Address {
      private String street;
      private String city;
      Address() {
      street="Wall Street";
      city="New York";
      }
      }

      Spring creates both objects and injects the Address object into the Employee constructor. There are 4 types of dependency injection:

      • Constructor Injection — dependencies provided as constructor parameters (Spring, Pico container, etc.)
      • Setter Injection — dependencies assigned through setter methods (Spring)
      • Field Injection — dependencies assigned directly through variables (Spring)
      • Interface Injection — done through an interface (Avalon, now closed)

      Note: Spring supports only Constructor and Setter Injection.

    • IoC Container — responsible for collaborating objects and managing their lifecycle. Two ways of collaborating objects: Dependency Pulling (Dependency Lookup, Contextual Dependency Lookup) and Dependency Injection (Setter, Constructor).

    • AOP — separates cross-cutting concerns (logging, caching, security) from business logic.

    • MVC Framework — for building web applications or RESTful services, capable of returning responses in different formats (JSON, XML, etc).

    • Transaction Management — a generic transaction management layer usable with or without a J2EE (JEE) environment; scales down to a local transaction and up to global transactions (using JTA).

    • Secure — Spring Security is a Java SE/Java EE security framework providing Authentication, Authorization, SSO, and more: Basic/Digest/Form-Based auth, LDAP, OpenID, SSO, CSRF protection, “Remember Me” via cookies, ACLs, automatic HTTP/HTTPS channel security, JAAS, flow authorization via Spring WebFlow, WS-Security via Spring Web Services.

    • Layered Architecture — several modules built on top of the core module; pick only the features you need.

    • Exception Handling — a convenient API for translating technology-specific exceptions into unchecked exceptions.

    • Easy Integration — designed to be used alongside ORM, Struts, Hibernate, and other frameworks, without imposing restrictions.

    • End-to-end development — usable for standalone apps, web apps, and applets.

  7. Can we say Spring is the replacement of Java EE? No — Spring is built on top of Java EE and internally uses it. Without Java EE, Spring wouldn’t exist. Spring complements Java EE rather than replacing it.

  8. What is POJO? POJO stands for Plain Old Java Object. A public class that doesn’t implement/extend a prespecified interface/class, has no prespecified annotation, and can be directly compiled and run under the JDK without any classpath reference to a library or framework.

  9. What is a Java Bean? A POJO with a stricter set of rules:

    • Properties should be private, exposed via getters/setters following getX()/setX() naming (or isX() for booleans).
    • A no-argument default constructor must be present.
    • Implementing Serializable allows storing its state.
  10. Similarities and differences between POJO & Java Bean?

    • Similarities: both classes must be public; properties in both must be private; both must have a default (no-argument) constructor.
    • Differences: a Java Bean must implement java.io.Serializable (not mandatory for POJOs); a Java Bean must have getters/setters (optional for POJOs).
    • Note: all Java Beans are POJOs, but not all POJOs are Java Beans.
  11. What is a Spring Bean? A class containing attributes and methods with business logic, whose object is instantiated, assembled, and otherwise managed by a Spring IoC container. There’s no restriction on what can become a Spring Bean — it can refer to any third-party interface/class/annotation.

  12. What is the IoC Container? A container of Spring Beans, responsible for creating objects, wiring them together, configuring them, and managing their complete lifecycle from creation to destruction. We provide Spring Beans (POJOs) and configuration metadata; the IoC container reads that metadata, wires the beans, manages their lifecycle, and hands us a fully configured, ready-to-use system.