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

Spring Autowiring in XML Configuration


Introduction

  • In simple dependency injection, we manually provide the dependencies using the ref attribute.
    Here are Program 1 & Program 2 of dependency injection.

  • However, with autowiring, we use the autowire attribute in <bean> tag, to let Spring automatically inject the dependencies.
  • Syntax : <bean id="--beanName--" class="--BeanClassName with package--" autowire="--autowiring modes--">

Modes of Autowiring :

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

1. no:
  • Definition : In this mode, no autowiring is performed. Dependencies must be explicitly defined using <property> or <constructor-arg> tags.
  • Example :
    <bean id="engine" class="com.example.Engine" />
    <bean id="car" class="com.example.Car">
        <property name="engine" ref="engine" />
    </bean>
  • Explanation : Here, no autowiring is used. The dependency (engine) is explicitly provided using the <property> tag. Spring will not attempt to inject dependencies automatically.
  • Internal Behavior : In this mode, no automatic injection is performed. All dependencies must be explicitly defined using <property> or <constructor-arg> tags in the XML configuration. Spring does not attempt to inject anything by itself.
  • Click Here for program.
2. byName:
  • Definition : In this mode, Spring will attempt to match the property name with a bean id in the container. If a match is found, it will inject the dependency.
  • Example :
    <bean id="engine" class="com.example.Engine" />
    <bean id="car" class="com.example.Car" autowire="byName" />
  • Explanation : Here, Spring will inject the engine bean into the car bean because the name engine matches the property name in the Car class.
  • Internal Behavior : By default, byName uses setter injection to inject dependencies. However, if the Java class has an explicit constructor that matches the bean, Spring may opt for constructor injection instead.
  • Click Here for program.
3. byType:
  • Definition : In this mode, Spring will inject the dependency based on the type of the property. If a bean of the matching type is found in the container, it will be injected.
  • Example :
    <bean id="engine" class="com.example.Engine" />
    <bean id="car" class="com.example.Car" autowire="byType" />
  • Explanation : Here, Spring will match the property type of the Car class (e.g., Engine type) and inject the corresponding bean (engine).
  • Internal Behavior : By default, byType relies on setter injection to match and inject dependencies based on their type. In cases where the class defines a suitable constructor, Spring can automatically switch to constructor injection.
  • Click Here for program.
4. constructor:
  • Definition : In this mode, Spring will inject the dependency through the constructor. If there are multiple constructors, Spring will try to find the one that matches the types of the parameters.
  • Example :
    <bean id="engine" class="com.example.Engine" />
    <bean id="car" class="com.example.Car" autowire="constructor" />
  • Explanation : Here, Spring will inject the engine bean into the Car bean's constructor based on the constructor parameter type.
  • Internal Behavior : In this mode, Spring always uses constructor injection. It resolves dependencies by matching the bean type to the constructor parameters. If multiple constructors exist, Spring tries to match the most specific one with the available beans.
  • Click Here for program.
5. autodetect:
  • Definition : This mode is a combination of constructor and byType. Spring will first attempt constructor autowiring. If it doesn't find a match, it will then try to use byType autowiring.
  • Example :
    <bean id="engine" class="com.example.Engine" />
    <bean id="car" class="com.example.Car" autowire="autodetect" />
  • Explanation : If the Car class has a constructor that can accept an Engine bean, Spring will use constructor injection. If no suitable constructor is found, it will fall back to byType.
  • Internal Behavior : This mode first attempts constructor injection. If a matching constructor is not found, it falls back to byType setter injection.
  • Note : It is deprecated since Spring 3.0.