Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Which type of bean injection is better or preferable and when?

Pragya Keshap answered on February 21, 2023 Popularity 2/10 Helpfulness 1/10

Contents


More Related Answers

  • What is the default scope of bean in the Spring framework
  • java bean
  • Question 1 In an XML file how is bean injection achieved?
  • What are different types of dependency injection supported by Spring?
  • @Bean java spring
  • set bean properties
  • What types of dependency injection are supported by Spring Framework? When do you use Setter and Constructor Injection, the pros and cons?
  • What is a BeanFactory Interface?
  • Explain the concept of a BeanFactory?
  • What is Inner bean? What is the drawback of inner bean?
  • The bean's scope
  • Annotation and beans
  • How bean is injected in spring? which type of dependency injection is you will prefer?
  • How bean is injected in spring? which type of dependency injection is you will prefer?
  • How bean is injected in spring? which type of dependency injection is you will prefer?
  • Explain about different types of beans.

  • Which type of bean injection is better or preferable and when?

    0

    The simple answer is - choose one style and use it consistently in our application. But each method has some benefits over another:

    Constructor based bean injection always provide an instantiated dependency. So we should use it in situations where the dependency is mandatory or required. While setter based bean injection gives the flexibility to instantiate the dependency at a later stage. So we should use it for optional dependencies. But remember that in case of setter, the dependency will/can be in the null state. So before using it you should always check for nulls or proper instantiation

    Setter injection gives flexibility by instantiating only a few dependencies. It does so by calling a particular setter for a property and not all. While constructor injection forces to instantiate all dependencies for a bean. Or we can say setter injection allows delayed instantiation of dependencies

    Setter injection has another advantage. We can always change a value assigned to a bean, compared to constructor injection. Which instantiates an object once and always return that reference throughout a bean life-cycle

    Historically, in earlier days people used setter injection more than constructor in Spring. And it is also more accustomed to how Spring framework works since everything is configurable flexibly  


    https://www.linkedin.com/pulse/different-types-dependency-injection-spring-kashif-masood/

    Popularity 2/10 Helpfulness 1/10 Language whatever
    Source: Grepper
    Tags: whatever
    Link to this answer
    Share Copy Link
    Contributed on Feb 21 2023
    Pragya Keshap
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.