,

Creating Spring Beans

There are several ways of defining Spring Beans in the Spring Framework. Each method has its own advantages and disadvantages. You may be wondering which one should you use. The answer is, it depends. The reality is in a large enterprise Spring project, you will likely encounter a combination of methods in defining Spring Beans. Neither is wrong, nor necessarily better. In legacy Spring applications, you’re likely to encounter XML based Spring Configuration. In new Spring applications, you’re likely to encounter more annotation based configurations and Java based configurations.

In this post, I’ll step you through common ways of defining Spring Beans. At the end of the post, I’ll show you how the different methods work seamlessly.

Say Hello Spring Bean

All the examples in this post will implement the following Interface. We will use simple Hello World type beans which will print a message to the system console.

Spring Configuration

XML Based Configuration

XML based Spring Configuration has been around since the beginning of Spring. It is the oldest and most mature method of defining Spring Beans.

Hello Spring Bean

Here is the Spring Bean we will use for this example.

Spring XML Configuration

Application

The following code can be used to execute the example Spring Bean.

Output

Java Based Configuration

In Spring 3, Java based configuration was released. The Spring community is gradually converting over to prefer the use of Java based configuration over XML based configuration. It is somewhat easier to use than XML. Since the configuration is in Java, you do have a tremendous amount of flexibility in defining beans. You’re no longer bound by the constraints of describing beans in XML, and have the full power of the Java language at your disposal.

Hello Spring Bean

Java Configuration Class

The following class is a Spring Configuration class. The annotation @Configuration  at the top of the class defines the class as a configuration class. The class will be picked up in a Spring component scan. It can also be imported into other configuration classes too. Objects returned  @Bean  annotation will be registered as Spring Beans. By convention the name of the registered Spring Bean will be set equal to the name of the method.  

Application

The following code can be used to execute the example Spring Bean.

Output

Annotation Based Configuration

Spring also can detect beans based on class level annotations. Any class annotated with with @Component , @Service , @Controller , or @Repository  will be considered a Spring Bean. The class must reside in a package which had been identified for a component scan. Spring will scan the package for annotated classes. Any class detected will be managed by the Spring Context at run time.

Hello Spring Bean

Application

The following code can be used to execute the example Spring Bean.

Output

Using Spring Bean Configurations Together

While there are different ways of defining Spring Beans, its important to remember the different methods can be used together. Its very common to use multiple methods of defining Spring Beans in a single application.

Spring Bean Application

We can use the beans previously defined to demonstrate how this works seamlessly. The Spring component scan functionality will detect classes annotated as Spring Beans, and classes marked as configuration classes which also define Spring Beans. In the following code example, the annotation @ComponentScan("guru.springframework") tells Spring to perform a component scan on the package guru.springframework.

This technique will work for annotated classes, but it not pick up XML configuration files. The annotation @ImportResource("classpath*:spring/spring-config.xml") will bring the specified xml file into the Spring context.

The class below is annotated to perform the component scan, which will pick up Spring Beans defined in Java configuration classes, Spring Beans defined with annotations, and Spring Beans defined in XML configuration files.

Output

When you run the above class, you will see the following output.

Get The Code

I’ve committed the source code for this post to github. It is a Maven project which you can download and build. If you wish to learn more about the Spring Framework, I have a free introduction to Spring tutorial. You can sign up for this tutorial in the section below.

Source Code

The source code for this post is available on github. You can download it here.

Video Code Review

I’ve posted a code review of this post on YouTube. In the review I step through the classes we used in this post.

Share

You May Also Like

Leave a Reply