In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shares the content of an example analysis of Spring dependency injection. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
I. Dependency profile
A typical enterprise application does not consist of a single object (or bean in Spring parlance). Even the simplest application has only a few objects working together to present what the end user sees as a coherent application. How to go from defining many separate bean definitions to fully implemented applications where objects collaborate to achieve goals.
For more information on spring design patterns and applications, please refer to my article Spring Common Design Patterns and Applications.
II. dependency injection
Dependency injection (DI) is a process by which objects can define their dependencies from factory methods through constructor parameters, factory method parameters, or properties set after an object instance is constructed or returned. The container then injects these dependencies at bean creation time. This process is fundamentally the opposite, hence the name Inversion of Control (IoC), which itself controls the instantiation or location of its own dependencies by using direct construction of classes or service locator patterns.
The code is clearer than the DI principle, and decoupling is more effective when objects provide dependencies. The object does not look up its dependencies and does not know the location or class of the dependencies. As a result, your classes become easier to test, especially when dependencies are on interfaces or abstract base classes that allow stub or mock implementations to be used in unit tests.
There are two main variants of DI, constructor-based dependency injection and setter-based dependency injection.
III. Constructor-based Dependency Injection
Constructor-based DI is accomplished by the container invoking a constructor with multiple parameters, each representing a dependency. Calling static factory methods with specific parameters to construct this bean is almost equivalent, and this discussion also treats parameters as constructors and static factory methods. The following example shows a class that can only be dependency injected through constructor injection. Note that there is nothing special about this class, it is a POJO and it does not depend on container-specific interfaces, base classes, or annotations.
public class SimpleMovieLister {
private MovieFinder movieFinder;public SimpleMovieLister(MovieFinder movieFinder) {this.movieFinder = movieFinder;}}1. Constructor parameter matching
Constructor parameter resolution matches using the type of the parameter. If there is no potential ambiguity in the constructor parameters of the bean definition, then the order in which the constructor parameters are defined in the bean definition is the order in which they are supplied to the appropriate constructor when the bean is instantiated.
package x.y;
public class Foo {public Foo(Bar bar, Baz baz) {// ... }}
No potential ambiguity exists, assuming Bar and Baz are not inherited. Therefore, following configuration well, you do not need to specify constructor parameters explicitly in index, type or indicator.
In the previous scenario, if you explicitly specify the type of the constructor parameter using the type attribute, the container can use a type matching the simple type.
Use the index property to specify the exponent of an explicit constructor parameter.
Constructor parameter name matching can also be used
2. Based on Setter Method
Bean-based calls to the Setter method are done by bean calls to the Setter method when calling a parameterless constructor or parameterless static factory method to instantiate the bean.
public class SimpleMovieLister {
private MovieFinder movieFinder;public void setMovieFinder(MovieFinder movieFinder) {this.movieFinder = movieFinder; }}
The Spring team generally advocates constructor injection because it enables an application component to be implemented as an immutable object and ensures that required dependencies are not null. In addition, constructor-injected components are always returned to client (calling) code in a fully initialized state. As an illustration, a large number of constructor arguments are a bad code smell, which means that classes may have too many responsibilities and should be properly separated by better problem solving.
Setter injection should be used primarily for optional dependencies that allow reasonable defaults to be assigned within a class. Otherwise, the non-null check must be performed wherever the code uses dependencies. The first advantage is that setter methods enable objects of this class to be reconfigured or reinjected. So management through JMX MBeans is a compelling use case for setter injection.
Thank you for reading! About "Spring dependency injection example analysis" This article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.