SpringBoot中使用纯scala进行开发 配置教程 非常简单的案例
- 新建项目
- 建好之后
建一个叫scala的文件夹
并把它标记为root文件夹
- 修改pom.xml文件
复制粘贴如下内容:
添加的插件的作用,如果不添加,在新建文件的时候右键只能新建java的文件,无法创建scala的class和object。添加插件之后就可以了。
<properties>
<scala.version>2.11.8</scala.version>
<spring.version>2.0.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<recompileMode>incremental</recompileMode>
<scalaVersion>${scala.version}</scalaVersion>
<launchers>
<launcher>
<id>app</id>
<mainClass>com.light.sword.ylazy.LightSwordApplication</mainClass>
<args>
<arg>-deprecation</arg>
</args>
<jvmArgs>
<jvmArg>-Xms256m</jvmArg>
<jvmArg>-Xmx2048m</jvmArg>
</jvmArgs>
</launcher>
</launchers>
</configuration>
<dependencies>
<!-- spring热部署-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 在scala(文件夹)下新建一个包,按照下图的层级关系,建好各个文件。
- 各个文件的内容
package xxproj
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
class AppConfig {
}
package xxproj
import org.springframework.boot.SpringApplication
object xxprojApplication extends App {
SpringApplication.run(classOf[AppConfig])
}
package xxproj.controller
import com.alibaba.fastjson.JSONObject
import org.springframework.web.bind.annotation.{RequestMapping, RestController}
@RestController
class indexController {
@RequestMapping(value = Array("/index"))
def index(): JSONObject = {
val json = new JSONObject
json.put("code", 0)
json.put("data", "success")
json
}
}
-
运行xxprojApplication
如果没报错就可以正常访问了 -
浏览器输入localhost:8080/index