Spark的一些基本情況如下:
Spark:一個java web框架
License:Apache License
服務器:Jettry
jre版本:8
github地址:https://github.com/perwendel/spark
准備工作:
下載並配置eclipse for javaee、maven、jdk1.8
大概的了解一下以下東西:
java8里的lambda表達式是什么
jetty是什么
jetty嵌入方式開發是什么樣的
環境搭建:
1,新建一個web maven工程
注:
若eclipse新建的maven工程並沒有包含web相關配置,可以新建一個web工程然后對工程右鍵configure --> convert to maven project,來轉換為maven工程即可。
檢查工程中是否有
src/mian/java、src/test/java、src/main/resources這幾個source folder,若沒有則在windows資源管理器中手動創建,然后回到工程打開“project視圖”,找到剛才新建的目錄,點擊右鍵”build path->Use as Source Folder
“將其作為source folder。
2,將工程編碼設置為utf-8,maven也是,如下
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
3,設置maven的使用java8編譯
<build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
4,加入Spark和日志類庫(
加入spark后會自動加入jettry的類庫
)
<dependencies> <dependency> <groupId>com.sparkjava</groupId> <artifactId>spark-core</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.21</version> </dependency> </dependencies>
5,編寫“hello world”
下面是
官方示例
package me.ooi.testSpark; import static spark.Spark.*; public class HelloSpark { public static void main(String[] args) { get("/hello", (request, response) -> "Hello World!"); } }
注:若你在 get("/hello", ......) ,這個地方感覺很奇怪,奇怪這個get是哪里來的,可以先看一下如下寫法
package me.ooi.testSpark; import spark.Spark; public class HelloSpark { public static void main(String[] args) { Spark.get("/hello", (request, response) -> "Hello World!"); } }
然后看一下
“
import
static
”的用處,就會明白了
6,執行這個main方法就可以訪問了
7,發布(發布為單個jar文件)
在pom.xml中添加如下配置以用於打成單個jar包(“
me.ooi.testSpark.HelloSpark
”這個文件是入口)
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<!-- This tells Maven to include all dependencies -->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>me.ooi.testSpark.HelloSpark</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
並將<packaging>war</packaging>改為<packaging>jar</packaging>
然后對工程右鍵“Run As -> maven clean”,若沒有問題,再“Run As -> maven install”,
若沒有報錯就ok了。
導出的jar包文件名形如“testSpark-0.0.1-SNAPSHOT-jar-with-dependencies.jar”這樣的。
運行:
在cmd窗口使用“java -jar testSpark-0.0.1-SNAPSHOT-jar-with-dependencies.jar”
停止:
直接關閉cmd窗口即可
注:若需要修改日志輸出參數,則需要在classpath下面新建一個名為simplelogger.properties(若使用的是
slf4j-simple
)的文件,下面是
slf4j-simple
官網上
可以設置的參數說明
#org.slf4j.simpleLogger.logFile - The output target which can be the path to a file, or the special values "System.out" and "System.err". Default is "System.err". #org.slf4j.simpleLogger.defaultLogLevel - Default log level for all instances of SimpleLogger. Must be one of ("trace", "debug", "info", "warn", or "error"). If not specified, defaults to "info". #org.slf4j.simpleLogger.log.a.b.c - Logging detail level for a SimpleLogger instance named "a.b.c". Right-side value must be one of "trace", "debug", "info", "warn", or "error". When a SimpleLogger named "a.b.c" is initialized, its level is assigned from this property. If unspecified, the level of nearest parent logger will be used, and if none is set, then the value specified by org.slf4j.simpleLogger.defaultLogLevel will be used. #org.slf4j.simpleLogger.showDateTime - Set to true if you want the current date and time to be included in output messages. Default is false #org.slf4j.simpleLogger.dateTimeFormat - The date and time format to be used in the output messages. The pattern describing the date and time format is defined by SimpleDateFormat. If the format is not specified or is invalid, the number of milliseconds since start up will be output. #org.slf4j.simpleLogger.showThreadName -Set to true if you want to output the current thread name. Defaults to true. #org.slf4j.simpleLogger.showLogName - Set to true if you want the Logger instance name to be included in output messages. Defaults to true. #org.slf4j.simpleLogger.showShortLogName - Set to true if you want the last component of the name to be included in output messages. Defaults to false. #org.slf4j.simpleLogger.levelInBrackets - Should the level string be output in brackets? Defaults to false. #org.slf4j.simpleLogger.warnLevelString - The string value output for the warn level. Defaults to WARN.