Spring Auto-Wiring Using Annotations
Spring Auto-Discovery of Beans
Typically in Spring, dependency injection is achieved using
However, in large applications, the number of beans will increase and the corresponding XML written to configure the numerous beans will become very large and unwieldly.
Spring has 3 ways to minimize XML in spring-config.xml:
- Using Auto-Wiring
- Using Auto-Discovery of Beans
- Using Java based configuration
In this tutorial, we shall focus on the point 3.
In Spring, as an alternative to XML, it is possible to declare beans and perform dependency injection in the Java tier. This is achieved by using
- context:component-scan element in XML to allows spring to look into java packages to search for annotated classes. It can also load the java configuration class.
- @Configuration annotation on a Java class to let Spring know that this class shall be used for defining beans. This class contains the bean definitions and can be considered similar to the <beans> element in the spring configuration file.
- @Bean annotation tells spring to register the returned object as a bean. This is like the <bean> element in the spring configuration file.
Sample Program Overview
The following sample program describes the details involved in creating Java based configuration using Spring.
We will create the Person class with members as name and email.
We will create the SpringJavaBasedConfig class which will act as the java based configurator. This class shall instantiate the Person class.
We will also create the spring-config.xml and use only the
element. We will not declare any beans in spring-config.xml
Finally, we will test our setup using TestJavaBasedConfiguration class which will load Spring context and get a reference to Person class. We will print Person.getName() and Person.getEmail() on the console to verify that Java based configuration using Spring has occured successfully.
Create the Person (see sample code below).
Create members name and email (see lines 5-6 below)
Create accessor methods for name and email (see lines 8-19 below).
Create the SpringJavaBasedConfig (see sample code below).
annotation to let Spring know that this class shall be used for Java based configuration.
Create method called person() (see lines 10-15 below) that shall be referred to for instantiating the Person class.
annotation to declare the Person class (see line 9 below).
Hence using the
annotations, we can perform Java based configuration using Spring.
Create the spring-config.xml file (see below).
Declare the tag
tag and provide the name of the package that Spring should look into for java based configuration (see line 14 below)
Finally, we need a java program to test the our setup.This is done by TestJavaBasedConfiguration (see source code below).
We need to tell Spring framework to use the ‘spring-config.xml’ to load our beans (see line 11 below).
We get the reference to Person class through Spring using the bean name ‘person’ (see line 12 below).
We access the Person.getName() and Person.getEmail() methods and print the output to verify that java based configuration using spring has occured successfully (see lines 13-14 below).
This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies) on your machine and automatically run the program for you as shown in the steps below. To run the sample program, you only need Java Runtime Environment (JRE) on your machine and nothing else.
(Alternatively you can go the folder containing the springjavaconfiguration-installer.jar and execute the jar using
java -jar springjavaconfiguration-installer.jar
This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
. All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.