1. Overview

This article illustrates some of the useful features of the Eclipse Spring Tool Suite (STS) IDE, which are useful when developing Spring applications.

First we show the benefits of using STS as compared to the traditional way of building applications with Eclipse.

Thereafter, we focus on how to bootstrap an application, how to run it and how to add additional dependencies. Finally we conclude by adding application arguments.

2. STS Main Features

STS is an Eclipse-based development environment that is customized for the development of Spring applications.

It provides a ready-to-use environment to implement, debug, run and deploy your applications. It also includes integration for Pivotal tc Server, Pivotal Cloud Foundry, Git, Maven and AspectJ. STS is built as an addition on top of the latest Eclipse releases.

2.1. Project Configuration

STS understands almost all of the most common Java project structures. It parses configuration files and then displays detailed information about beans that are defined, dependencies, used namespaces and in addition extracts overviews for certain stereotypes.

spring-bean-snapshot

2.2. STS Features Overview

Eclipse STS validates your project and provides quick fixes for your applications. For example, when working with Spring Data JPA, the IDE may be used to validate query method names (more on this in section 6).

STS also provides a graphical view on all bean methods and their mutual relationships. You may want to have a closer look at the graphical editors that come with STS by looking into the views that are available under the menus window, show view and then Spring respectively.

STS also offers other additional useful features that are not limited to Spring applications only. The reader is recommended to a look at the full list of features that can be found here.

3. Creation of a Spring Application

Let us start by bootstrapping a simple application. Without STS a Spring application is usually created by using the Spring Initializer website or the Spring Boot CLI. This may be simplified by clicking on Create Spring Starter Project from your dashboard in STS.

In the New Spring Starter Project screen either use the defaults or make your own adjustments and then go to next screen. Select Web and click finish. Your pom.xml should now look similar to this:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.1.5</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
        
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Your version of Spring Boot may be different but the latest version may always be found here.

4. Running the Application

The aforementioned application may be started by right-clicking on the project and selecting run as Spring Boot App. Without STS, you will most likely run the application from the command line with the following command:

$ mvn spring-boot:run

By default, Spring applications are started with Tomcat running on port 8080. At this point, the application starts on port 8080 and basically does nothing else as we have not implemented any code yet. Section 8 shows you how to change the default port.

5. Logging and ANSI Console

When you run the project from the IDE using the run command, you will notice that the console outputs some nice color-coded log statements. In case you want to turn it off, go to run configurations… and disable the check box Enable ANSI console output on the Spring Boot tab. Alternatively, you can also disable it by setting a properties value in the application.properties file.

spring.output.ansi.enabled=NEVER

More information on the configuration of your application logs may be found here.

6. JPA Query Name Checks

At times implementing a data access layer may be a cumbersome activity. A lot of boilerplate code may have to be written to realize simple queries and perform pagination. Spring Data JPA (JPA) aims to significantly facilitate such an implementation of data access layers. This section illustrates some of the benefits of using JPA in conjunction with STS.

To get started, add the following dependency for JPA to the previously generated pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>

You may have noticed that version has not been specified in the above declaration. This is due to the fact that dependencies are managed by the spring-boot-starter-parent.

To make JPA work, it is required that you properly define your entity managers and transaction managers. However, Spring auto-configures these for you. The only thing left to the developer is to create the actual entity classes. These entities are managed by the entity manager, which in turn is created by the container. Let us for example create an entity class Foo like so:

@Entity
public class Foo implements Serializable {
    @Id
    @GeneratedValue
    private Integer id;
    private String name;

    // Standard getters and setters
}

The container scans all classes annotated with @Entity from the root of the configuration package. Next we create a JPA repository for the Foo entity:

public interface FooRepository extends JpaRepository<Foo, Integer> {
    public Foo findByNames(String name);
}

At this point you may have noticed already that the IDE now flags this query method with an exception:

Invalid derived query! No property names found for type Foo!

This is of course due to the fact that we have accidentally written an ‘s’ in the method name of the JPA repository. To fix this remove the spurious ‘s’ like so:

public Foo findByName(String name);

Notice that no @EnableJpaRepositories was used on the config class. This is because the container’s AutoConfigration pre-registers one for the project.

“Jar Type Search” is a feature that was introduced in STS 3.5.0. It provides content-assisted proposals in projects for classes that are not (yet) on the classpath. STS may help you adding dependencies to your POM file in case they are not yet on the classpath.

For example, let us add a line to the Foo entity class. For this example to work properly, please ensure first that the import statement for java.util.List is already present. Now we may add Google Guava as follows:

private List<String> strings = Lists // ctrl + SPACE to get code completion

The IDE will suggest several dependencies to be added to the classpath. Add the dependency from com.google.common.collect, press return and add the dependency from Guava. The Guava jar will now automatically be added to your pom.xml file like so:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

As of version STS 3.8.0, you get a confirmation dialog box before STS makes its changes to your pom.xml.

8. Adding Application Arguments

One of the other powerful features of Spring is the support of external configurations which can be passed to an application in several ways, e.g. as command-line arguments, specified in properties or YAML files or as system properties. In this section, we focus on adding a configuration option as application start argument using STS. This is illustrated by configuring Tomcat to start on a different port.

In order to run an application on a Tomcat port other than the default, you may use the command below, where a custom port is specified as command-line argument:

mvn spring-boot:run -Drun.arguments="--server.port=7070"

When using STS, you have go to the run menu. Select run configurations… from the Run Configurations dialog, select Spring Boot App from the left panel and select demo – DemoApplication (this will be different if you did not select the default project). From (x)= Arguments tab type in the Program Arguments window

--server.port=7070

and run. You should see output in your console similar to the output that is shown below:

.
.
2016-07-06 13:51:40.999  INFO 8724 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 7070 (http)
2016-07-06 13:51:41.006  INFO 8724 --- [           main] com.baeldung.boot.DemoApplication        : Started DemoApplication in 6.245 seconds (JVM running for 7.34)

9. Conclusion

In this article we have shown the basics of developing a Spring project in STS. Some of the things we have shown are execution of applications in STS, support during the development of Spring Data JPA and the usage of command-line arguments. However, there are many more useful features that may be employed during development as STS offers a rich set of features.

The full implementation of this article can be found in the github project – this is an Eclipse based project, so it should be easy to import and run as it is.