Three ways to inject in Spring IOC

Source: Internet
Author: User
Tags what interface

Project Error Knowledge Point record

Body

Recently in the project, with the spring framework, the Spring Framework provides an IOC auto-injection feature that can easily help us create a bean, so that we save the code for the new Object (). IOC provides three kinds of injection methods, interface injection, set method injection and constructor injection, three kinds of injection methods are very easy to use, the specific use of the internet there are many, we can search Baidu by themselves ...

That day when I was using interface injection, I found out that idea gave me a warning (I've had this warning before, but I didn't pay much attention to it), and I looked at it so that I could use the constructor injection method. It's confusing to me that I used to know that there were three injections in the IOC, but they never understood the difference (always feeling injected, as long as they could inject beans). But through the idea of the hint, I found that this thing is not simple, so I have a good study of these three kinds of injections, after all, the enemy can Baizhanbudai ~

First, let's introduce the IOC injection method, there are three ways to inject the IOC,

    1. Interface injection: If an interface is injected into a bean, then the injected bean will have to implement this interface (this is very overbearing right, I want to implement what interface, also need to specify) ...
    2. Set method Injection: If you inject a bean with a set, then you just need to provide a set method for some of the components needed in the bean, and the Set method injection is clearer, and everyone knows (oh ~ Originally you want this bean to provide these components) ...
    3. Constructor injection: If you are using a constructor injection, first provide a custom constructor for the bean, and the parameters required in the constructor are the component instances in the class.

These three methods compare each other, you will find that interface injection is obviously not good, as I said above is too overbearing, and the set method is better than interface injection, but we consider a situation, that is, if the bean needs too many components, then we need to provide a lot of set method .... It crashes, this class can be very long ... Very long .... The constructor injection method is the official spring recommendation, because it only needs to provide a constructor, first of all the code is not much, followed by the set method to provide users with too much information, for example, if the bean requires 3 components, and use this set only 2 of these components, So when you call this bean, it's possible .... Error. This is obviously a disadvantage, in the design class, we all know the bottom of the package, do not let the user know too much of the class information ...

In summary, is a warning to bring me the knowledge ...

Three ways to inject in Spring IOC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.