๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Spring Interview Questions - 4


16. What is Autowiring in Spring, and what are its benefits ?
  • Autowiring is a Spring feature that automatically resolves and injects bean dependencies into a class.
  • It eliminates the need for explicit configuration of dependencies in XML or Java-based configuration.
  • Benefits of Autowiring
    1. Encourages Loose Coupling: Dependencies are injected dynamically, promoting design flexibility and better modularity.
    2. Reduces Boilerplate Code: Simplifies dependency injection by eliminating repetitive and verbose configuration.
    3. Improves Readability and Maintainability: Makes the configuration cleaner, more concise, and easier to understand.
17. What are the different DI Mechanisms that autowiring follows and which mechanism is recommended to use ?

There are 3 DI Mechanisms that autowiring follows :-

  1. Constructor Injection:
    • Autowires dependencies via the class constructor.
    • How to use:
      • XML: autowire="constructor" attribute in <bean> tag.
      • Java Annotation: @Autowired annotation on the constructor.
    • Recommendation:
      • Use it for mandatory dependencies.
      • Ensures immutability and clear dependency requirements at object creation.
  2. Setter Injection:
    • Autowires dependencies via public setter methods.
    • How to use:
      • XML: autowire="byType" or autowire="byName" attribute in <bean> tag.
      • Java Annotation: @Autowired annotation on the setter method.
    • Recommendation:
      • Use it for optional dependencies.
      • Offers flexibility but makes dependencies mutable.
  3. Field Injection:
    • Autowires dependencies directly into fields.
    • How to use:
      • XML: Not typically used in XML.
      • Java Annotation: @Autowired annotation directly on the field.
    • Recommendation:
      • Avoid it when possible.
      • Breaks encapsulation, makes testing harder, and is less maintainable.
18. What are the modes of autowiring in XML-based configuration ?

Total there are 5 modes of autowiring in xml configuration which are listed as below :-

  • no (Default Mode)
    • Description: No autowiring is performed. Dependencies must be explicitly defined using or tags.
    • Example:
      <bean id="engine" class="com.example.Engine" />
      <bean id="car" class="com.example.Car">
          <property name="engine" ref="engine" />
      </bean>
  • byName
    • Description: Matches bean names with property names to inject dependencies.
    • Example:
      <bean id="engine" class="com.example.Engine" />
      <bean id="car" class="com.example.Car" autowire="byName" />
  • byType
    • Description: Matches bean types with the type of properties to inject dependencies.
    • Example:
      <bean id="engine" class="com.example.Engine" />
      <bean id="car" class="com.example.Car" autowire="byType" />
  • constructor
    • Description: Injects dependencies into a bean's constructor by matching argument types.
    • Example:
      <bean id="engine" class="com.example.Engine" />
      <bean id="car" class="com.example.Car" autowire="constructor" />
  • autodetect (Deprecated)
    • Description: Automatically detects whether to use constructor or byType for autowiring.
    • Example:
      <bean id="engine" class="com.example.Engine" />
      <bean id="car" class="com.example.Car" autowire="autodetect" />
19. What is the role of the autowire-candidate attribute in XML configuration ?
  • Purpose:
    • It is used to indicate whether a bean should be considered as a candidate for autowiring.
    • If it is set to false, then the bean will not be eligible for autowiring, even if its type or name matches other beans in the container.
  • Default Behavior:
    • By default autowire-candidate is true.
    • It means that by default, all beans are considered eligible for autowiring, even though it's not explicitly mentioned in the XML configuration.
  • When to Use:
    • We should use it(autowire-candidate=false) when we have multiple beans of the same type and we want to prevent a specific bean from being injected automatically.
  • Example:
    • <bean id="userService" class="com.example.UserService" autowire="byType"/>
      <bean id="userRepository" class="com.example.UserRepository" autowire-candidate="false"/>
      In this case, userRepository will not be autowired into userService despite matching by type.
  • Benefit:
    • It provides control over which beans are autowired, avoiding conflicts when there are multiple beans of the same type in the container.
20. What are the limitations of using XML-based autowiring compared to explicit wiring ?

Some limitations or disadvantages of XML-based autowiring are as follows :-

  • Naming Conventions Dependence:
    • It relies on naming conventions (in the case of byName), which can lead to runtime errors if there is no matching bean.
  • Ambiguity with Multiple Beans:
    • Ambiguity can arise if there are multiple beans of the same type (for byType), causing conflicts during autowiring.
  • Less Flexibility:
    • It is less flexible and harder to debug compared to explicit wiring methods, such as constructor or setter injection.
  • Lack of Fine-Grained Control:
    • Does not support fine-grained control over dependency injection like annotations (e.g., @Autowired), which offer more flexibility.