Spring Understanding
I first learnt Spring in 2008 when I was a student, and I remember that day had a verify famous pattern named SSH, that including,
-
Spring
Spring is used for manage beans, compared with EJB way, it's very easy for a beginner like me to learn and use.
-
Struts
Apache Struts was used for MVC, although, it's outdated with current Spring MVC, but in that time, it's much better than using JSPs.
-
Hibernate
Hibernate is never changed, it's always used as an ORM
After many years experience with Spring, and Spring boot, I am coming up with my mind,
What is Spring
Spring Framework provides Dependency Injection and IOC containers, Spring MVC flow, AOP, and several useful API for Java programmers. Spring Framework also integrates with many other excellent third-party modules, such as,
- Lombok
- Hibernate
- Flyaway
- JOOQ
- Kafka
- Redis
- RabbitMQ
- ActiveMQ
- Quartz
It's more like a standard for Java Programers, with Spring, we can develop from small to enterprise applications.
What modules included in Spring
- The Spring Core Container
- Beans
- Core
- Context(ApplicationContext)
- Validation and Data Binding and Type Conversion
- Spring Expression Language(SpEL)
- AOP
- Database JDBC abstraction and DAO module
- Web/MVC
How to use/configure Spring
Without maven and gradle in the old days, we need to download the Spring and other used jars manually, and add them into the project library path, its painful right.
Now, we can use maven and gradle to manage the dependencies, and the Spring officials provides start.spring.io to easily create a Spring project and choose the module we want to include.
What is DI
When a class A depends on another class B, we say B is the A's dependency. And when we creating class A instance, we must to create its dependency class B instance first. This is highly coupled, and it also violates the SOLID principles.
Dependency Injection is a design pattern that allows injecting dependency on Objects, instead of object resolving the dependency. That's the biggest pros of the Spring framework.
What is IoC
The simple meaning of inversion of the control means that now the framework, Spring is responsible for creating objects, wiring dependencies, and managing their life-cycle instead of a developer, which was the case before. That’s where control is inverted from developer to framework.
Instead manage the dependencies directly with new operator, we declare the dependencies for a bean through constructor or setter method by configuring in XML or use annotation, and the Spring framework will be responsible for managing the dependencies.
What is AOP
Aspect-oriented Programming (AOP) complements Object-oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns (such as transaction management) that cut across multiple types and objects. (Such concerns are often termed "crosscutting" concerns in AOP literature.)
AOP is used in the Spring Framework to:
- Provide declarative enterprise services. The most important such service is declarative transaction management.
- Let users implement custom aspects, complementing their use of OOP with AOP.
What is joint Point
A point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution.
What is advice
Action taken by an aspect at a particular join point. Different types of advice include around, before, and after advice.
What is Pointcut
A predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.
What is Spring boot
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". Create stand-alone Spring applications
-
Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
Spring Boot provides a number of embedded web servers that you can use to run your application. This means that you don't need to set up a separate web server to run your application.
-
Provide opinionated 'starter' dependencies to simplify your build configuration
Spring Boot provides a number of starter dependencies that you can add to your project to get started with common Spring features, such as web development, data access, and security.
-
Automatically configure Spring and 3rd party libraries whenever possible
Spring Boot automatically configures your application based on the dependencies you have added to your project. This means that you don't have to spend time manually configuring your application.
-
Provide production-ready features such as metrics, health checks, and externalized configuration
Spring Boot provides a number of production-ready features, such as health checks, metrics, and logging. This makes it easy to deploy your application to production.
-
Absolutely no code generation and no requirement for XML configuration
What is component scanning
Annotation-based container is an alternative to XML setup is provided by annotation-based configuration, which relies on bytecode metadata for wiring up components instead of XML declarations. 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.
Annotation injection is performed before XML injection. Thus, the XML configuration overrides the annotations for properties wired through both approaches.
The process of searching the classpath for annotated configuration classes that should be part of the application context to is called component scanning.