Creating Spring Beans

1 Comment

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.

package guru.springframework.hello;

/**
 * Created by jt on 3/28/15.
 */
public interface HelloBean {
    void sayHello();
}

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.

package guru.springframework.xml;

import guru.springframework.hello.HelloBean;

/**
 * Created by jt on 3/28/15.
 */
public class XmlBean implements HelloBean {
    @Override
    public void sayHello() {
        System.out.println("Hello, I'm a XML Bean");
    }
}

 

Spring XML Configuration

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

       <bean id="xmlBean" class="guru.springframework.xml.XmlBean"/>
</beans>

 

Application

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

package guru.springframework.xml;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportResource;

/**
 * Created by jt on 3/28/15.
 */
@SpringBootApplication
@ImportResource("classpath*:spring/spring-config.xml")
public class XmlApplication {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(XmlApplication.class, args);
        XmlBean bean = (XmlBean) ctx.getBean("xmlBean");
        bean.sayHello();
    }
}

 

Output

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.2.RELEASE)

2015-03-28 10:38:02.260  INFO 66700 --- [           main] guru.springframework.xml.XmlApplication  : Starting XmlApplication on Johns-MacBook-Pro.local with PID 66700 (/Users/jt/src/springframework.guru/blog/springbeans/target/classes started by jt in /Users/jt/src/springframework.guru/blog/springbeans)
2015-03-28 10:38:02.319  INFO 66700 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]3e57cd70: startup date [Sat Mar 28 10:38:02 EDT 2015]; root of context hierarchy
2015-03-28 10:38:03.229  INFO 66700 --- [           main] o.s.b.f.xml.XmlBeanDefinitionReader      : Loading XML bean definitions from URL [file:/Users/jt/src/springframework.guru/blog/springbeans/target/classes/spring/spring-config.xml]
2015-03-28 10:38:03.656  INFO 66700 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2015-03-28 10:38:03.669  INFO 66700 --- [           main] guru.springframework.xml.XmlApplication  : Started XmlApplication in 1.813 seconds (JVM running for 2.311)
Hello, I'm a XML Bean
2015-03-28 10:38:03.670  INFO 66700 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]3e57cd70: startup date [Sat Mar 28 10:38:02 EDT 2015]; root of context hierarchy
2015-03-28 10:38:03.671  INFO 66700 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

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

package guru.springframework.javaconfig;

import guru.springframework.hello.HelloBean;

/**
 * Created by jt on 3/28/15.
 */
public class JavaConfigBean implements HelloBean {
    @Override
    public void sayHello() {
        System.out.println("Hello, I'm a Java Config 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.

package guru.springframework.javaconfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by jt on 3/28/15.
 */
@Configuration
public class JavaConfig {
    @Bean
    public JavaConfigBean javaConfigBean(){
        return new JavaConfigBean();
    }
}

Application

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

package guru.springframework.javaconfig;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;

/**
 * Created by jt on 3/28/15.
 */
@SpringBootApplication
@Import(JavaConfig.class)
public class JavaConfigApplication {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(JavaConfigApplication.class, args);
        JavaConfigBean bean = (JavaConfigBean) ctx.getBean("javaConfigBean");
        bean.sayHello();
    }
}

 

Output

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.2.RELEASE)

2015-03-28 11:13:46.387  INFO 66847 --- [           main] g.s.javaconfig.JavaConfigApplication     : Starting JavaConfigApplication on Johns-MacBook-Pro.local with PID 66847 (/Users/jt/src/springframework.guru/blog/springbeans/target/classes started by jt in /Users/jt/src/springframework.guru/blog/springbeans)
2015-03-28 11:13:46.447  INFO 66847 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]3e57cd70: startup date [Sat Mar 28 11:13:46 EDT 2015]; root of context hierarchy
2015-03-28 11:13:47.579  INFO 66847 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2015-03-28 11:13:47.607  INFO 66847 --- [           main] g.s.javaconfig.JavaConfigApplication     : Started JavaConfigApplication in 1.651 seconds (JVM running for 2.265)
Hello, I'm a Java Config bean
2015-03-28 11:13:47.609  INFO 66847 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]3e57cd70: startup date [Sat Mar 28 11:13:46 EDT 2015]; root of context hierarchy
2015-03-28 11:13:47.610  INFO 66847 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

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

package guru.springframework.componentscan;

import guru.springframework.hello.HelloBean;
import org.springframework.stereotype.Component;

/**
 * Created by jt on 3/28/15.
 */
@Component
public class ComponentScanBean implements HelloBean {
    public void sayHello(){
        System.out.println("Hello, I'm a Component Scan Bean");
    }
}

Application

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

package guru.springframework.componentscan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

/**
 * Created by jt on 3/28/15.
 */
@SpringBootApplication
public class ComponentScanApplication {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(ComponentScanApplication.class, args);
        ComponentScanBean bean = (ComponentScanBean) ctx.getBean("componentScanBean");
        bean.sayHello();
    }
}

 

Output

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.2.RELEASE)

2015-03-28 15:21:17.576  INFO 67246 --- [           main] g.s.c.ComponentScanApplication           : Starting ComponentScanApplication on Johns-MacBook-Pro.local with PID 67246 (/Users/jt/src/springframework.guru/blog/springbeans/target/classes started by jt in /Users/jt/src/springframework.guru/blog/springbeans)
2015-03-28 15:21:17.632  INFO 67246 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]3e57cd70: startup date [Sat Mar 28 15:21:17 EDT 2015]; root of context hierarchy
2015-03-28 15:21:18.850  INFO 67246 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2015-03-28 15:21:18.867  INFO 67246 --- [           main] g.s.c.ComponentScanApplication           : Started ComponentScanApplication in 1.671 seconds (JVM running for 2.218)
Hello, I'm a Component Scan Bean
2015-03-28 15:21:18.869  INFO 67246 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]3e57cd70: startup date [Sat Mar 28 15:21:17 EDT 2015]; root of context hierarchy
2015-03-28 15:21:18.870  INFO 67246 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

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.

package springbeans;

import guru.springframework.componentscan.ComponentScanBean;
import guru.springframework.hello.HelloBean;
import guru.springframework.javaconfig.JavaConfigBean;
import guru.springframework.xml.XmlBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@ComponentScan("guru.springframework")
@ImportResource("classpath*:spring/spring-config.xml")
public class SpringBeansApplication {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(SpringBeansApplication.class, args);

        List<HelloBean> helloBeans = new ArrayList<HelloBean>();
        helloBeans.add((ComponentScanBean) ctx.getBean("componentScanBean"));
        helloBeans.add((JavaConfigBean) ctx.getBean("javaConfigBean"));
        helloBeans.add((XmlBean) ctx.getBean("xmlBean"));

        for(HelloBean helloBean : helloBeans){
            helloBean.sayHello();
        }
    }
}

 

Output

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

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.2.RELEASE)

2015-03-28 15:47:11.531  INFO 67265 --- [           main] springbeans.SpringBeansApplication       : Starting SpringBeansApplication on Johns-MacBook-Pro.local with PID 67265 (/Users/jt/src/springframework.guru/blog/springbeans/target/classes started by jt in /Users/jt/src/springframework.guru/blog/springbeans)
2015-03-28 15:47:11.602  INFO 67265 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]3e57cd70: startup date [Sat Mar 28 15:47:11 EDT 2015]; root of context hierarchy
2015-03-28 15:47:12.544  INFO 67265 --- [           main] o.s.b.f.xml.XmlBeanDefinitionReader      : Loading XML bean definitions from URL [file:/Users/jt/src/springframework.guru/blog/springbeans/target/classes/spring/spring-config.xml]
2015-03-28 15:47:12.725  INFO 67265 --- [           main] o.s.b.f.xml.XmlBeanDefinitionReader      : Loading XML bean definitions from URL [file:/Users/jt/src/springframework.guru/blog/springbeans/target/classes/spring/spring-config.xml]
2015-03-28 15:47:12.753  INFO 67265 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'xmlBean': replacing [Generic bean: class [guru.springframework.xml.XmlBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [file:/Users/jt/src/springframework.guru/blog/springbeans/target/classes/spring/spring-config.xml]] with [Generic bean: class [guru.springframework.xml.XmlBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [file:/Users/jt/src/springframework.guru/blog/springbeans/target/classes/spring/spring-config.xml]]
2015-03-28 15:47:13.014  INFO 67265 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2015-03-28 15:47:13.028  INFO 67265 --- [           main] springbeans.SpringBeansApplication       : Started SpringBeansApplication in 1.878 seconds (JVM running for 2.474)
Hello, I'm a Component Scan Bean
Hello, I'm a Java Config bean
Hello, I'm a XML Bean
2015-03-28 15:47:13.029  INFO 67265 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.spring[email protected]3e57cd70: startup date [Sat Mar 28 15:47:11 EDT 2015]; root of context hierarchy
2015-03-28 15:47:13.030  INFO 67265 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

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.

About jt

    You May Also Like