Hello World in Spring

August 8, 2016

In this lesson of the Spring tutorial, we will create a simple HelloWorld example. To do this, we need to cover at first the topic of the bean in Spring. Beans are just object instances that are managed by Spring. The IoC (Inversion of Control) container, which is one of the core parts of Spring, is responsible for managing the lifecycle of a bean.

1. Spring configuration

To define a bean in Spring you need to use Spring configuration. You have three types of configuration in Spring:

  • Java-based configuration (also known as JavaConfig),
  • Annotation-based configuration,
  • XML-based configuration.

At the beginning of this tutorial, we will focus on Java-based configuration. We do that because it has many advantages over different styles of configuration. One of the benefits is that when you refactor your classes, your IDE will automatically update your JavaConfig.

2. Java-based configuration example

2.1 HelloWorldPrinter class

To our example, we will use the HelloWorldPrinter class that just print “Hello world” String in its print method.

2.2. Configuration class

To create a JavaConfig class, you just create a standard class and annotate it with @Configuration annotation. When annotating a class with this annotation, you say to Spring, that this class contains one or more beans. So let’s create a class named HelloWorldConfiguration and annotate it with @Configuration.

To define a bean inside a Configuration class, you create a method and annotate it with @Bean. A name of the method indicates a bean id, which is used to identify a bean. As you can see inside of this method, we just return a new HelloWorldPrinter object instance.

2.3. HelloWorldExample class

Now we will call the print method of the HelloWorldPrinter class. Let’s create a class named HelloWorldExample.

Inside the main method of this class, at first, we create a context using the AnnotationConfigApplicationContext class. Application context in Spring is responsible for beans creation. AnnotationConfigApplicationContext is one of the implementations of ApplicationContext, which supports, apart from others, beans defined in classes annotated with @Configuration. As you can see, we pass to the constructor of this class our configuration class, HelloWorldConfig.

Next, we’ve used this context to get the bean of type HelloWorldPrinter. As you remember, we defined a method that returns a bean of this type in our configuration class HelloWorldConfig. The last line of the main method calls the print method. And as you may think, the result of running the main method of HelloWorldExample class is:

3. Summary

In this lesson of the Spring tutorial we’ve learned what is a bean in Spring. We’ve found out what are the types of configurations that you can use in Spring. Later I’ve shown you how to define a Spring bean using Java-based configuration. At the end we’ve created an example that loads a Spring configuration class and calls a bean method.

Related Posts
Click here to add a comment

Leave a comment: