I created a Hello World project with Spark Framework, which is a lightweight java web framework, and Spring Boot, which provides rich functions necessary for development, and compared what I felt.
--Create a Gradle project with an appropriate name from "File"-> "New Gradle Project" in eclipse
--Edit build.gradle
.
build.gradle
dependencies {
compile 'com.sparkjava:spark-core:2.8.0'
//Library required for Spark log output
compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.25'
}
--Create a main class.
SparkApplication
package spark.sample;
import static spark.Spark.get;
/*
* This Java source file was generated by the Gradle 'init' task.
*/
public class SparkApplication {
public static void main(String[] args) {
get("/", (req, res) -> {
return "HelloWorld Spark";
});
}
}
--Place log4j config file in src / main / resources
log4j.properties
log4j.rootCategory=INFO, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.err
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n
This completes the implementation of Hello World in Spark. Running the main class launches the built-in jetty.
--Check with browser
--Startup time: Approximately 590ms --Response time per request: Approximately 7ms
-Create a template with Spring Initializer and download the zip.
--Extract the zip downloaded earlier, and import it with "File"-> "import"-> "Exisiting Gradle Project" in eclipse.
--Create a Controller class.
SampleController
package spring.sample.sample;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SampleController {
@GetMapping("/")
public String index() {
return "HelloWorld SpringBoot";
}
}
This completes the implementation of Hello World in Spring. When run with SpringBootApp, the built-in tomcat will start.
--Check with browser
--Startup time: Approximately 6s --Response time per request: Approximately 12ms --Since favicon.ico is acquired by default, it is almost the same as Spark except for that.
--Advantages compared to Spring Boot ――If there is no complicated processing, development seems to proceed faster than Spring. --Development can be started if you know the lambda notation of java8 --Lightweight with few dependent modules --Disadvantages compared to Spring Boot --Since there is no enforcement by the framework in a good sense such as forcing the MVC pattern, it may not be suitable for large-scale development in the sense that you can write as much as you like. ――Since the functions required for an enterprise web application are not a full set, you need to implement or select the technology yourself.
I heard that the processing speed of an application created with SpringBoot will be slower than that of an application implemented with pure java because objects created on the framework side will be generated, and a lighter web frame. I wondered if there was any work, so I found the Spark framework and tried it.
I'll try to implement a simple business logic and if I notice it, I'll post it again.
Recommended Posts