## Notes - Dependency Injection - Aspect-oriented Programming - Transaction Management - Spring MVC - Spring Security - Spring Data - Spring Batch - Integration with other Frameworks ## Key Features - POJO-based Development - Modular Design - pick what you need - Framework Integration - works with JDBC, JPA, Hibernate, etc - Easy Testing - Can easily mock classes, inputs, outputs - Web MVC - Model-View-Controller - Centralized Error Handling - IoC containers are lightweight ## Benefits - Dependency Injection provides for loose-coupling; more easily tested and maintained - Aspect-oriented programming: separates cross-cutting concerns (logging, security, transaction management, etc) from business logic - Transaction management - Spring MVC: framework for building web APIs - Spring Security: provides common libraries and logic for handling authentication, authorization, and other common vulnerabilities (injection attack) - Spring Data: simplifies database access - Spring Batch: handles large-scale batch processing ## Dependency Injection Is a pattern for implementing Inversion of Control. It allows a class to get their dependencies from outside, rather than having to create them from within. - Constructor Injection: dependent object is passed in when an instance of the class is created - Setter Injection: the class allows for the developer to set the dependency as needed; the dependency can be changed as needed - Field Injection: `@Autowired` annotation on a property allows the ## Inversion of Control Container The control of object creation and dependency injection is moved externally from code. ### BeanFactory Simplest container that lazy-initializes beans. ```java Resource resource = new ClassPathResource("beans.xml"); BeanFactory factory = new XmlBeanFactory(resource); MyBean obj = (MyBean) factory.getBean("myBean"); ``` ### ApplicationContext A more advanced container that *extends* [[#BeanFactory]] and provides additional features - Internationalization - Event propagation - AOP ## Spring Annotations Metadata to define common behavior. - `@Component`: tells Spring that this class should be a Spring bean; makes it available for classpath scanning - `@Autowired`: Automatically injects dependencies into a class. - `@Bean`: Define a Spring bean explicitly within a configuration class. Used to create and configure beans not automatically detected by classpath scanning - `@Configuration`: Tells Spring that this class contains bean definitions and configuration. ## Spring Modules - Spring AOP: Allows defining `@Aspects` to handle cross-cutting concerns - Spring ORM: APIs for database interactions using Hibernate, JDO; simplifies transaction management and exception handling - Spring Web MVC: MVC architecture; [[DispatcherServlet]] - Spring DAO: data access support through JDBC, Hibernate, or JDO - Spring Application Context Module: Internationalization, validation, event propagation, resource loading ## References - [Spring - Geeks for Geeks](https://www.geeksforgeeks.org/advance-java/spring/)