Spring Book – Chapter 5 – Application Configuration – Simplified

XML Simplification Using Autowiring

Spring provides a mechanism for autowiring dependencies, either by type or by name. Spring can scans its configuration file at runtime, looking for a class of the same type as a property found on your main object. Once found, it creates an instance of that type and then injects that instance into the object’s property.

Listing 5-37. Conventional/traditional way of declaring beans in XML configuration

[xml]<bean id="customerRepository" class="com.mybook.loyalty.customer.CustomerRepositoryImpl" />

<bean id="loyaltyService" class="com.mybook.loyalty.services.LoyaltyServiceImpl">

<property name="customerRepository" ref="customerRepository" />

</bean>[/xml]

Here, a bean of the “customerRepository” type is injected into a bean of the “loyaltyService” type, assuming there is a property and setter method in the LoyaltyServiceImpl class:

Listing 5-38. Corresponding Java code required for, when Listing 5-37 XML configuration is used for configuring beans

[java]public class LoyaltyServiceImpl {

private CustomerRepository customerRepository;

public CustomerRepository getCustomerRepository () {

return customerRepository;

}

public void setCustomerRepository (CustomerRepository customerRepository) {

this. customerRepository = customerRepository;

}

//…other getters, setters, and methods

}[/java]

By using @Autowired, you can eliminate the additional XML in your configuration file that specifies the relationship between two objects. Also, you no longer need methods to set the property in the owning class. Just include a private or protected variable and Spring will do the rest for you using its container capabilities. Modified XML configuration is shown in Listing 5-39.

Listing 5-39. simplified XML configuration using @Autowired in Java class

[xml]<context:annotation-config />

<bean id="loyaltyService" class="com.mybook.loyalty.services.LoyaltyServiceImpl" />

<bean id="customerRepository" class="com.mybook.loyalty.customer.CustomerRepositoryImpl" />[/xml]

And the java code for this is shown in Listing 5-40 below.

Listing 5-40. Java Class showing the usage of @Autowired annotation

[java]import org.springframework.beans.factory.annotation.Autowired;

public class LoyaltyServiceImpl implements LoyaltyService{

@Autowired

private CustomerRepository customerRepository;

}[/java]

@Autowired is not just for property injection but also can be used in methods and constructors. You can potentially reduce a great deal of XML and code using this technique. Having said this, autowiring can cause problems in more complex object models and in bigger enterprise applications. When using @Autowired, if one and only one type you are looking for cannot be found, errors result. This is Spring’s improvement over traditional autowiring methodologies which will, by default, fail silently when an exact match cannot be found while doing the autowiring. Fortunately, with @Autowired, when two objects are of the same type you can add a ‘Qualifier’ attribute to identify the objects by both type and name:

Listing 5-41. Using @Autowired annotation with @Qualifier annotation

[java]@Autowired

@Qualifier("customerRepository")

private CustomerRepository customerRepository;[/java]

As one would expect, @Autowired is not a one stop shop for simplifying all your XML configurations, and it might not make sense for every situation. You can easily fall back to traditional configuration when @Autowired does not fit for your enterprise application development. However, @Autowired is appropriate option for developers building Spring applications.

Page Visitors: 3744

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 5 – Application Configuration – Simplified

  1. Hi Tomcy,

    All looks great and thanks for your time and efforts.

    One small suggestion if you could provide single page view for all the Spring tutorials would be great. May print option as well.

    Thanks & regards,
    AA

Leave a Reply

Your email address will not be published. Required fields are marked *