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.

zrzut-ekranu-2017-01-21-12-09-49

zrzut-ekranu-2017-01-21-12-10-16

zrzut-ekranu-2017-01-21-12-10-29

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

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.5.RELEASE</version>
</dependency>

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

Przechwytywanie.PNG

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

Przechwytywanie.PNG

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

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

    @Override
    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 Config.java we will have only package-scoped class with two annotations:

@Configuration
@ComponentScan
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 Main.java we need to have main method with only two lines:

ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
System.out.println(ctx.getBean(Person.class));

First line will set up Spring Context (annotation-based of course) with @Configuration annotated class (Config.java 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
Person{name=’Seba’}

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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