Java Spring & Boot Cheat Sheet
Java Spring & Boot Cheat Sheet
Bean scopes in the Spring Framework determine the lifecycle and visibility of bean instances. The default Singleton scope creates a single instance per Spring IoC container, which is shared across the application, conserving memory but limiting use in stateful scenarios. Prototype scope generates a new instance every time a bean is requested, useful for stateful or multi-threaded use cases. Other scopes like Request, Session, and Application are applicable for web-aware applications, respectively linking bean lifecycles to HTTP requests, sessions, or the servlet context, enhancing modular design for web components .
Dependency Injection (DI) and Inversion of Control (IoC) are key to the Spring Framework's architecture. DI is the process by which the Spring container injects object dependencies into a class, allowing for decoupled code. This is often done using @Autowired annotations. IoC, a broader concept, delegates object creation and lifecycle management to the Spring container, enhancing modularity by decoupling configuration from application logic. This approach facilitates easier unit testing and maintainability .
The @RestController annotation in Spring Boot simplifies the creation of REST APIs by combining the @Controller and @ResponseBody annotations. It marks a class as a web request handler and automatically serializes responses into JSON format. Within a @RestController, HTTP methods are mapped using annotations like @GetMapping and @PostMapping, which streamline the handling of client requests. This annotation enhances readability and reduces the complexity of writing REST services by automatically handling serialization and mapping .
The Java Collections Framework consists of interfaces and classes to handle data as objects in different structures. The main components include Lists, Sets, Queues, and Maps. Lists, such as ArrayList and LinkedList, maintain order and allow duplicates. Sets, such as HashSet and TreeSet, do not allow duplicates, offering unique elements. Queues, such as LinkedList and PriorityQueue, follow FIFO order and are used for processing data sequentially. Maps, such as HashMap and LinkedHashMap, manage key-value pairs efficiently but are not part of the Collection interface hierarchy .
The Inversion of Control (IoC) mechanism in the Spring Framework enhances application modularity by decoupling the instantiation and management of objects from the application code to the Spring container. Developers specify dependencies in configuration, allowing the framework to manage them, which simplifies testing as dependencies can be mocked or replaced easily. IoC facilitates changes in configuration without altering application logic, thus promoting reusability and increasing testability by enabling isolated component testing without considering their dependencies .
Maven and Gradle are both prominent build systems for managing dependencies and project configuration in Spring Boot. Maven uses a structured convention-over-configuration style, making it easy for new developers to understand a project setup. Its extensive repository of plugins and predefined goals accelerates builds but can become verbose. Gradle provides more flexibility and is faster due to its incremental builds and configuration-on-demand. However, its syntax can be challenging for beginners. Both systems facilitate efficient management of dependencies and project lifecycle in Spring Boot projects .
Spring Boot simplifies Java application development by providing pre-configured templates and minimizing boilerplate configuration. It supports embedded server set-up for easier deployment without needing separate server setup. Spring Boot’s auto-configuration feature detects and configures classes depending on dependencies present in the classpath, facilitating rapid application development. Additionally, Spring Boot's use of build systems like Maven or Gradle for dependency management and the introduction of CommandLineRunner and RESTful service annotations streamline build and deployment processes .
Annotations in the Spring Framework, such as @Component, @Service, @Repository, and @Controller, streamline the configuration by reducing the need for verbose XML configuration files. They allow developers to annotate classes with roles directly, enabling Spring's automatic scanning and registration of beans. For dependency injection, annotations like @Autowired simplify the injection process. The use of annotations like @PostConstruct and @PreDestroy facilitates lifecycle management by defining initialization and destruction callbacks. Overall, annotations enhance readability, reduce boilerplate code, and facilitate faster application development .
@PostConstruct and @PreDestroy annotations manage bean life-cycle events in Spring applications. @PostConstruct is used for initializing a bean after its properties have been set but before the bean is made available for use, suitable for any initialization tasks. @PreDestroy is used for custom cleanup tasks before the bean is destroyed, ensuring resources are released appropriately. These annotations ensure a clear and structured lifecycle phase handling, promoting resource management and reliability in Spring applications .
The Comparable interface is used for natural ordering of objects by implementing the compareTo() method, suitable when there is a single logical way of comparing objects, like sorting integers or alphabetically. Comparator, on the other hand, is used for custom orderings where multiple sorting criteria are needed, allowing different sorting strategies using the compare() method without altering the object’s class. This is useful for sorting complex user-defined objects by various attributes, such as sorting a list of employees by age or name. Utilizing these interfaces enhances the flexibility and reusability of the sorting logic in Java Collections .