JavaRush /Java Blog /Random EN /Benefits of using Spring
Alex
Level 37

Benefits of using Spring

Published in the Random EN group
Many novice developers sooner or later come across Spring , a popular framework for developing applications in Java. The main thing that Spring allows you to do is to simplify the development of J2EE applications for the developer. Here are the main benefits that a developer gets when using Spring:
Benefits of using Spring - 1
  • Spring provides a framework for your future application, if you want a “blank” for your future application. At the same time, the framework dictates the rules for building the application - there is a certain application architecture into which you need to build your functionality. This functionality will actually be the business logic of your application. Spring includes many subprojects tailored to specific functionality (SpringMVC, Spring Security, SpringData, etc. the full list can be seen at https://spring.io/projects ), from which the developer can choose the one most suitable for him, and do not use the rest - this is a modular principle of building an application;

  • In a Spring based application, objects are loosely coupled through the use of dependency injection. One of the goals of Spring was to break the dependency of some objects on others. What is addiction? This is when Object1 uses the methods of another Object2, i.e. Object1 depends on Object2, whose methods it uses. Why does he depend? But because until the object Object2 is created, Object1 will not be able to implement its functionality. How to break addiction? “Inject” a reference to the Object2 object into the Object1 object via a constructor or setter. This process is actually dependency injection. It is important to remember that in Spring objects must be built on the basis of interfaces, so that dependencies are injected in the form of an interface for possible subsequent replacement of the implementation.

  • you don't have to create objects manually using the new operator. This function has been delegated to the Spring container. This is inversion of control (IoC) - transferring the function of instantiating the necessary dependencies (objects) to the container. What is the developer's role in all this you ask? Declare the component so that it falls into the Spring context. Spring's context, simply put, is a map where all the beans are located. When they say that a bean is in the context of Spring, consider that the bean is in the map, and Spring knows the key to get it from the map. Everything that is marked as a bean in the xml configuration or in classes with @Component annotations is instantiated and placed in a map of the form Map<key,bean> map, i.e. the container has a map where it “stores” all the beans (the key concept in Spring is a bean, this is an entity managed by the container. In order for a bean (ordinary class) to become managed, it must fall into the Spring context.) and, if necessary, implementation, the container does something like this: map.get(key), the key is the field type;

  • Spring frees you not only from the need to create objects, but also to associate them. For example, the @Autowired annotation allows you to automatically wire components. The @Autowired spring annotation could be described simply like this: dear friend, spring container, please look in your map with beans to see if you have the class instanceof or implements of what I’m standing in front of. If there is, give me a link in the field before which I am declared. Automatic linking allows you to reduce the amount of code when determining component dependencies;

  • In Spring, bean settings are separated from the program code. Moving the configuration (dependency management) into a separate file makes it easier for subsequent changes in the project (replacing implementations):


    • improved testability. When classes are designed based on DI and interfaces, it becomes possible to easily replace dependencies (with fake implementations) during testing;

    • the ability to program in a declarative style using annotations reduces the amount of code in the application;

    • support and good integration with data access technologies, transactions, AOP simplifies development;

    • good documentation helps a lot when debugging an application;
For those who want to understand Spring, I recommend reading K. Walls. Spring in Action - 3rd Edition and of course the official documentation.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION