Bean Wiring

The process of creating and managing association among application objects forms the core of DI and is commonly referred to as wiring.

To inject dependencies into the application objects, you need to configure them in an XML-based configuration file.

In this file, you should first give a definition for the bean for which the dependency is to be created.

Example two beans addressBean is wired (linked) with studentBean.

<bean id="employeeBean"
<property name="name" value="Ravi"/>
<property name="age" value="25"/>
<property name="address" ref="addressBean"/>
<bean id="addressBean" 
<property name="city" value="ABC"/>
<property name="state" value="Haryana"/>

Bean wiring can be done in following ways:

  • Explicit wiring
  • Autowiring

Explicit wiring : In this case, the users explicitly declare the bean dependencies in the configuration file.

Autowiring : In this case, the bean dependencies are not stated explicitly. Instead, the container automatically determines the appropriate dependencies to inject

You can explicitly wire the beans by declaring their dependencies in the configuration file. While declaring the beans in the configuration file, you can also wire its properties.

You can explicitly wire the bean properties in the configuration file by:

  • Using setter injection.
  • Using constructor injection