Spring framework in 2 minutes

מאת HagayTech
בתאריך 10 מאי, 2020

mini-tutorial about the Spring framework What is IOC and DI and how they help us https://www.linkedin.com/pulse/2min-spring-framework-tutorial-hagay-onn-the-spot-/

Spring framework in 2 minutes

I was asked about Spring today, so I'm sharing here a mini-tutorial about the framework. Have Fun and many Smiles, because the Spring/Winter is coming (choose yours ;-), Hagay.

What is Spring? A lightweight (only few MB), open-source development framework, mainly used for building web applications, on top of the Java-SE/EE platform. Spring’s main leading features/concepts are:

  • Inversion of control (IOC) / Dependency-Injection (DI) - gives Loose Coupling between objects in the system, because is managed by Spring. Each class declares its dependencies instead of creating and managing the needed objects itself. IOC/DI enables lazy-loading of beans (just-in-time) and creates singletons by default.
  • MVC Framework - divides the application into 3 independent sections of the Data (Model), the GUI/UX (View) and the App’s Engine/Logic (Controller). This way each section is easier to develop and maintain with effects on other parts of the system.
  • Aspect Oriented Programming (AOP) - separates application’s business-logic from system services. Aspects can be configured to run an Advice (Callback function) before and/or after a method-call, to enable functionality needed among many classes, like logging, encrypting, zipping or role-based access-control mechanism. The extra layered functionality is called Cross-Cutting Concern and is useful for serving non-functional requirements and applying them over many classes, without touching their inner functional code (useful for 3rd parties).
  • Transaction Management - consistent scalable transaction management interface.
  • Exception Handling - convenient API that translates 3rd-party libraries specific-exceptions (thrown by JDBC/Hibernate for example) into consistent, unchecked exceptions.
  • Container - management and configuration of application objects.

Dependency-Injection (DI) - The BeanFactory is used to create and configure a full application. The Factory design-pattern separates the objects creation, configuration and dependencies from the application’s code. There are several available Bean-Factories, where the most commonly used currently is the org.springframework.beans.factory.xml.XmlBeanFactory. This container loads the application beans configuration based on XML file with tagged definitions. Each bean is declared in an XML <bean> tag definition and includes its configuration and initialization metadata, needed to start up an instance. The application itself is a bean.

Each bean has an attribute named "singleton" (true by default) that specifies if the bean should be a singleton, else it becomes a prototype/instance bean.

Each bean has a scope attribute that defines how to instantiate the bean objects. The 2 most common values of the scope are prototype and singleton. Prototype scope is used when a new bean instance to be produced each time, while singleton is used when the same instance of a bean must be returned every time. As mentioned Spring Beans are singletons by default.

The dependency-injection of beans can be defined the container invokes a class constructor with arguments, or as Setter-based by calling setter methods on beans after invoking a No-Argument Constructor or no-argument static factory method to instantiate the bean. Constructor-based DI is more suitable for mandatory dependencies, while Setter-based is used for optional dependencies.

 DAO (Data-Access /s) are used with JDBC to abstract the database access/query code and maintain it in one place. Other frameworks, like Hibernate/JDO, are enabled using the ORM module.

That's all for today, See ya next post! Hagay

My LinkedIn: https://www.linkedin.com/in/hagaytech
You can also connect with me on Twitter: https://twitter.com/HagayTech
Facebook: https://www.facebook.com/HagayTech
or watch some of my photos at https://gurushots.com/hagayo/photos

מאמרים נוספים...