Engineering Full Stack Apps with Java and JavaScript
A Spring Boot standalone application uses the @SpringBootApplication annotation over the main class, which also has the main method to 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.
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()");
}
}
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.
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>