Quantcast
Channel: Blog – FIVE
Viewing all articles
Browse latest Browse all 95

Spring Boot

$
0
0

In a world of constant new requirements and application development, I have always found it important to deploy new applications to test servers as soon as possible, even if they have minimal functionality. It gives you an opportunity to reveal potential context path problems and to eliminate ‘it works on my machine’ mysterious bugs.

1

If you are a Java developer using Spring framework, I believe you would be interested in an open-source framework that minimizes the need for excessive Spring configuration files and simplifies creating stand-alone applications that you can run in one click.

What is Spring Boot?

2

When you are developing an application, you will surely think about its architecture and logical components. We can say that majority of apps are roughly divided into three layers:

  • User Interface layer,
  • Business Logic (Service) layer and
  • Database layer.

To be as flexible as possible, Spring Boot is also divided into several components which you can include in your project. Therefore, you do not need to include whole Spring Boot library, just those components which are necessary  for your type of application.

Here are the most important components:

  • spring-boot-starter – includes basic Spring classes from Spring Core, which makes it only mandatory component and is included automatically,
  • spring-boot-starter-web – for developing web applications,
  • spring-boot-starter-data-jpa – database communication,
  • spring-boot-starter-security – managing user authorization and authentication,
  • spring-boot-starter-aop – Aspect Oriented Programming support and
  • many more…

Full list of Spring Boot components is available here (https://github.com/spring-projects/spring-boot/blob/master/spring-boot-dependencies/pom.xml).

placerholder_402

Configure!
So what makes Spring Boot so cool? I believe its feature to scan your project and guesses which beans it needs to create to make your app work as expected.

Of course, this guessing is not left to chance but it uses this bulletproof method:

  • scans Spring Boot components included in your project,
  • scans all Spring beans (@Component, @Service etc) you have defined in your project and
  • uses scan result to create new beans.

Not clear? Let me give you an example. You are building Spring based web application. Naturally, you have read previous chapter of this blog and know that you must include spring-boot-starter-web component. To save you the trouble, Spring Boot will automatically create beans of  ViewResolver, ObjectMapper, TemplateEngine if they are found on your classpath. Needles to say, you are ready to create your HTML page and write Controller to supply it.

Does you app need to communicate with database? Include spring-boot-starter-data-jpa and Spring Boot will know that it needs to initialize DataSource, TransactionManager, EntityManager and several other classes. All you need to do is provide database connection information.

Define, package, start!

Picture above shows project structure when using Spring Boot. You have your Java code in java folder, and resources folder for your static documents. If you are building a web application, Spring Boot will require that you have 2 folders:

  • templates – contains HTML files for your web page
  • static – for static files which are accessible via HTTP requests, like CSS or Javascript files.
4

One extra, very useful, file you can include is application.properties. It is simple properties file which will be read and loaded into system properties upon application start and before any bean initialization. There are also predefined property keys which Boot will recognize, as described below.

server.port=5555 // application will start on port 5555 (8080 by default)
spring.datasource.url=jdbc:h2:~/springboot;AUTO_SERVER=TRUE // db name
spring.datasource.username=user // db username
spring.jpa.hibernate.ddl-auto=update // hibernate command

As mentioned before, Spring Boot philosophy is “keep it simple!”. With that in mind, it packages your application into a big fat JAR with all dependent libraries and embedded Tomcat or Jetty. That means you can run it with simple java -jar command. And it runs in under 15 seconds, depending on your database connection.
Like all executable JAR files, you must run them via main method in which you use Spring Boot static method to start your app.

@EnableAutoConfiguration
@ComponentScan
@Configuration
public class Starter {

  public static void main(String[] args) {
    SpringApplication.run(Starter.class, args);
  }

Just be sure to put three essential annotations:

  • EnableAutoConfiguration – instructs Spring Boot to use its ability to scan and auto-init beans,
  • ComponentScan & Configuration – declares that this class is also a Spring configuration file.

Deploy and enjoy!

5
There are many services like Amazon Web Services, Heroku, Cloud Foundry or CloudBees which provides you with Linux server containers for your apps. You are given direct access to these containers so you can easily deploy and manage your applications. Also, each of these service providers has an excellent tutorials and customer support so you will not have any troubles in starting your app.

The post Spring Boot appeared first on Five.


Viewing all articles
Browse latest Browse all 95

Trending Articles