Master java skills

Autowiring

Bean dependencies can be wired implicitly as well. This is known as Autowiring. There are few modes of wiring, enabling which will enable autowiring of bean dependencies.

Autowiring modes

1. No

No means autowiring is not enabled automatically. Dependencies need to be wired explicitly.

2. byName

A dependency is autowired by its name in this mode. The only thing to remember is that the id of the injected bean should be exactly same as the name of the property in the java class. Let’s understand this with example

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="emp" class="com.sks.Employee" autowire="byName">
		<property name="id" value="10"></property>
		<property name="name" value="Mark"></property>
		<property name="age" value="25"></property>
		<property name="contract" value="true" />
	</bean>
	<bean id="add" class="com.sks.Address">
		<property name="addLine" value="Street 1"></property>
		<property name="city" value="London"></property>
		<property name="country" value="UK"></property>
	</bean>

</beans>

3. byType

In this mode, dependency is autowired by matching its type. It is checked if there is a type of a bean in the spring container or not. If yes, it will be autowired. In this mode, name of the bean is not matched with the name of the property in the java class.

Note -> In this mode, there should be exactly one bean of the type which is to be autowired, otherwise, there would be an ambiguity.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="emp" class="com.sks.Employee" autowire="byType">
		<property name="id" value="10"></property>
		<property name="name" value="Mark"></property>
		<property name="age" value="25"></property>
		<property name="contract" value="true" />
	</bean>
	<bean id="add" class="com.sks.Address">
		<property name="addLine" value="Street 1"></property>
		<property name="city" value="London"></property>
		<property name="country" value="UK"></property>
	</bean>

</beans>

4. Constructor

In constructor autowiring mode, the dependency is injected via constructor with max number of parameters.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="emp" class="com.sks.Employee" autowire="constructor">
		<constructor-arg type="int" value="10"></constructor-arg>
		<constructor-arg type="String" value="Mark"></constructor-arg>
		<constructor-arg type="int" value="25"></constructor-arg>
		<constructor-arg type="boolean" value="true" /></constructor-arg>
	</bean>
	<bean id="add" class="com.sks.Address">
		<constructor-arg type="String" value="Street 1"></constructor-arg>
		<constructor-arg type="String" value="London"></constructor-arg>
		<constructor-arg type="String" value="UK"></constructor-arg>
	</bean>

</beans>