Your First Spring Boot Application

A Spring Boot standalone application uses the @SpringBootApplication  annotation over a class with a main  method that will execute the application. The SpringApplication.run method call accepts two parameters — the class that actually contains the annotated @SpringBootApplication annotation and any application arguments. The ApplicationArguments interface allows you to access any application arguments.

 

Bean Components

We will use a simple bean class based on our previous examples:

package com.javajee.springboot;

import org.springframework.stereotype.Component;

@Component
class JJWriter {
    public void write() {
        System.out.println("Inside JJWriter's method write()");
    }
}

 

Test Class (Main class)

A Spring Boot standalone application can be created using the @SpringBootApplication.

JJWriterMain.java

package com.javajee.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class JJWriterMain {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(JJWriterMain.class, args);
        JJWriter writer = context.getBean("JJWriter", JJWriter.class);
        writer.write();
        context.close();
    }
}

Before executing the program, you must have also configured your Gradle/Maven dependencies.

 

Maven POM File

You need to add Spring Boot dependencies instead of Spring dependencies. Spring Boot provides easy to use starter pom dependencies such as spring-boot-starter, spring-boot-starter-test, spring-boot-start-web.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.javajee.springbootdemo</groupId>
  <artifactId>springbootdemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>springbootdemo</name>
  <description>springbootdemo</description>
  
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
</dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  
</project>

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!