In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the reasons for the use of constructor injection in javaspring. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
I. Preface
The importance of Spring framework to Java development is self-evident. Its core features are IOC (Inversion of Control, inversion of Control) and AOP, the most commonly used of which is IOC. By handing components to the IOC container of Spring, we control the dependency of objects by Spring to avoid excessive program coupling caused by hard coding.
Second, three common injection methods
2.1 field injection
@ Controllerpublic class FooController {@ Autowired / / @ Inject private FooService fooService; / / A simple use example is the same as public List listFoo () {return fooService.list ();}}
This kind of injection should be the most common injection method that the author has seen in the development so far. The reason is simple:
The injection method is very simple: add the field to be injected, append @ Autowired, and you can do it.
It makes the whole code simple and clear, and looks beautiful and generous.
2.2 Constructor injection
@ Controllerpublic class FooController {private final FooService fooService; @ Autowired public FooController (FooService fooService) {this.fooService = fooService;} / / same as in usage, slightly}
This is the injection method recommended in the Spring4.x version, which is a bit ugly compared to the above field injection method, especially when the injection has a lot of dependencies (more than 5), you will obviously find that the code is very bloated
2.3 setter injection
@ Controllerpublic class FooController {private FooService fooService; / / in the same way, @ Autowired public void setFooService (FooService fooService) {this.fooService = fooService;}}
In the early days of Spring3.x, injection was recommended, but the constructor injection parameters were so cumbersome that it was cumbersome, and setter could be used to make classes reconfigure or re-inject later.
III. Benefits of constructor injection
This constructor injects in a way that ensures that the injected components are immutable and that the required dependencies are not empty. In addition, the dependency injected by the constructor can always guarantee a fully initialized state when the client (component) code is returned
Dependency immutable: I'm actually talking about the final keyword, which I won't explain any more here. Gardeners who don't understand can go back and have a look at Java grammar. Dependency is not empty (omitting us to check it): when you want to instantiate FooController, you will not call the default constructor because you have implemented the constructor with parameters, so you need to pass the required parameters in the Spring container, so there are two cases: 1. There are parameters of this type-> passed in, OK. 2: no parameter of this type-> error is reported. So guarantee will not be empty, Spring will not send a null into it. Fully initialized state: this can be combined with the above dependency is not empty, before passing parameters to the constructor, to ensure that the injection is not empty, then be sure to call the constructor of the dependent component to complete the instantiation. In the process of loading instantiation of the Java class, the constructor is the last step (previously, if there is a parent class, initialize the parent class first, then its own member variables, and finally the constructor, which is not expanded in detail here). So what comes back is the state after initialization.
This is the end of the article on "why javaspring uses constructor injection". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.