Java, Spring Framework

Simplest introduction to Spring

Nowadays most of companies demand from Java developers three hard skills:

  • Java (of course)
  • Hibernate
  • and finally Spring

Today, I am going to show you how to set up the easiest application using maven, IntelliJ IDEA and Spring. Source of code, that is shown below is also stored on my github. Enjoy.

One more thing beforehand. You may ask what Spring actually is. I am not going to explain it to you here, but direct you to official page.

Let’s start!

First thing you need to do is install or run some IDE if you have it already. My choice is IntelliJ IDEA as mentioned before. When it is up you have to create maven project like on screens presented below.




When you do it, the next step is to add dependency into pom.xml:


We don’t need to add all dependencies for Spring, because IntelliJ will manage to download all demanded ones for us:


Now create some package and put there three classes as shown below:


In we will have some POJO with one field, which is name:

class Person {
    private final String name = "Seba";

    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +

The only new thing here is annotation @Component. It says to Spring, that this class demands creation of object inside of current running context.By default this object (bean) will be in singleton scope, so there will be only one Person in whole application. It should not be like that, because in real life application components are not entities nor DAOs, but for this example is sufficient. More about beans scopes here.

In we will have only package-scoped class with two annotations:

class Config {

@Configuration tells us, that current class is only for Spring purpose and will hold beans definitions (annotated with @Bean). In our example there are no beans in there, but will will get back to them in another post. For now it is good to know, that everything in Spring in the latest versions can be defined in classes rather than in xml files like it was previously.

When you add @ComponentScan annotation without any other parameters on class, it tells Spring Context, that it should look for beans annotated with @Component inside current package. To look for beans in other packages you should pass their directories as params of annotation like here:

@ComponentScan(basePackages = {"com.springsample"})

In our case @ComponentScan determines, that there is bean Person configured outside of configuration class.

In we need to have main method with only two lines:

ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);

First line will set up Spring Context (annotation-based of course) with @Configuration annotated class ( in out case) and in the second line we will call toString() method of bean Person, which is retrieved from Spring Context with method getBean().

Finally, we can now run safely our application. Result of it should be the following:

org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1a6c5a9e: startup date [Sat Jan 21 12:58:00 CET 2017]; root of context hierarchy


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s