【scala】使用gradle和scala构建springboot程序

零、版本说明:

springboot: 2.7.18

使用log4j2,不使用springboot自带的logback

scala版本:2.11

jackson版本:2.16.0

一、依赖:

groovy 复制代码
buildscript {
    dependencies {
        // using spring-boot-maven-plugin as package tool
        classpath("org.springframework.boot:spring-boot-maven-plugin:2.7.18")
    }
}
plugins {
    id 'idea'
    id 'scala'
    id 'org.springframework.boot' version '2.7.18'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
// 指定java版本
sourceCompatibility = 1.8
targetCompatibility = 1.8
// 尽量使用2.16.0版本的jackson对scala支持更好。
ext {
    jackson_version = '2.16.0'
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web") {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
    implementation("org.springframework.boot:spring-boot-starter-log4j2")
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude group: 'org.junit.jupiter'
    }
    testImplementation 'io.projectreactor:reactor-test:3.4.29'
    implementation("com.fasterxml.jackson.core:jackson-core:${jackson_version}")
    implementation("com.fasterxml.jackson.core:jackson-annotations:${jackson_version}")
    implementation("com.fasterxml.jackson.core:jackson-databind:${jackson_version}")
    implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:${jackson_version}")
    implementation("com.fasterxml.jackson.module:jackson-module-scala_2.11:${jackson_version}")

    testImplementation "com.fasterxml.jackson.core:jackson-core:${jackson_version}"
    testImplementation("com.fasterxml.jackson.core:jackson-annotations:${jackson_version}")
    testImplementation("com.fasterxml.jackson.core:jackson-databind:${jackson_version}")
    testImplementation("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:${jackson_version}")
    testImplementation("com.fasterxml.jackson.module:jackson-module-scala_2.11:${jackson_version}")
}
configurations {
    all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}

主启动类

此处也可以继承scala的App 但需要注意要重新App中的main方法。

scala 复制代码
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.{ SpringBootApplication}

@SpringBootApplication
class AppServe
object AppServer {
  private val log = org.slf4j.LoggerFactory.getLogger(classOf[AppServer])

  def main(args: Array[String]): Unit = {
    log.info(s"${getClass.getName} get args: ${args.toList.toString()}")
    SpringApplication.run(classOf[AppServer], args: _*)

  }
}

springboot序列化使用的是jackson,而自带的jackson版本较低,且不支持scala。

scala 复制代码
import com.fasterxml.jackson.databind.Module
import com.fasterxml.jackson.module.scala.{ClassTagExtensions, DefaultScalaModule}
import org.springframework.context.annotation.{Bean, Configuration}
import lombok.extern.slf4j.Slf4j

/**
 * https://blog.csdn.net/beibaozhou1656/article/details/100966023
 */
@Configuration
class JacksonConfiguration {
  @Bean
  def defaultScalaModule(): Module = {
     DefaultScalaModule::ClassTagExtensions
  }
}

controller类

注意:@Resource()@Autowire)使用方法。

scala 复制代码
import org.springframework.web.bind.annotation.{PostMapping, PutMapping, RequestBody, RequestMapping, RestController}
import lombok.extern.slf4j.Slf4j

@Slf4j
@RestController
@RequestMapping(Array("/v1"))
class BatchController @Resource()(batchService: BatchSaveService) {

  @PostMapping(Array("/batch/save"))
  def batchSave(@RequestBody tbls: java.util.List[Element]): Unit = {
    batchService.batch(tbls.asScala)
  }
}

配置文件值获取

application.properties,application.yml,application.yaml文件

scala 复制代码
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import javax.annotation.PostConstruct

@Slf4j
@Component
class KerberosEnvConfiguration() {
// 注意:要去除value=s"${}"的`s`。
  @Value(value = "${udf.kafka.consumer.krb5-conf}") var krb5Conf: String = _
  @PostConstruct
  def init(): Unit = {
 // 使用在post construct之后可以使用krb5Conf变量
 System.setProperty( ... )
  }
  def getConf():String={
  krb5Conf
}
}
scala 复制代码
import lombok.extern.slf4j.Slf4j
import org.apache.kafka.clients.consumer.ConsumerRecord
import org.springframework.kafka.annotation.KafkaListener
import org.springframework.stereotype.{Component}
import javax.annotation.Resource

/**
 * kerberos整合kafka、springboot
 * https://blog.csdn.net/weixin_40496191/article/details/124056953
 * SpringBoot集成Kafka详解
 * https://blog.csdn.net/qq_20865839/article/details/13394898
 */
@Slf4j
@Component
class KafkaTableService @Resource()(service: CommonServiceImpl)  extends Logging {
  val mapper = JsonUtils.getMapper
// 可以使用#{}在注解中使用被@Component的的方法。
  @KafkaListener(topics = Array("#{kerberosEnvConfiguration.getConf()}"))
  def onMessage(record: ConsumerRecord[String, String]) = {
  // do nothing
  }
  
}

注意:

1、springboot打包必须使用spring-boot-maven-plugin,不能使用shadowJar

如下jar目录结构:

2、idea中debug
3、无需设置main-class,springboot的插件设置。

4、生产环境启动nohub java -jar xxx.jar 2>&1 &

参考文章:
<>

相关推荐
腾渊信息科技公司11 小时前
Spring Boot集成TDengine实战:工业时序数据存储选型与迁移方案
spring boot·后端·tdengine
xiaoqiMikko1 天前
Dependabot 面板全绿,不代表你的 Tomcat 没洞
java·spring boot
摇滚侠1 天前
SpringBoot 官网 阅读笔记 启用生产就绪功能 端点
spring boot·笔记·后端
evans在进步1 天前
HashMap 为什么线程不安全?ConcurrentHashMap 如何解决?
java·spring boot·spring
小林敲代码77881 天前
Spring Boot 3 升级踩坑:aj-captcha 行为验证码底图加载失效
java·spring boot·后端
程序员阿明1 天前
spring boot3访问resources下的文件,使得在浏览器url中可以直接访问
java·spring boot·后端
霸道流氓气质1 天前
SpringBoot中使用OAuth2 认证与 JWT Token — 概念、原理与实践
java·spring boot·后端
snow@li1 天前
Vue Axios封装与SpringBoot Payload封装全景关联分析(前后端数据交互底层闭环)
前端·vue.js·spring boot
用户3126874877201 天前
Spring Boot 日志体系到底怎么加载的?从 Logback 到 MDC 的全链路拆解
spring boot
paopaokaka_luck1 天前
基于springboot3+vue3的云南本土影视文旅推荐平台(协同过滤算法、Echarts图形化分析)
前端·spring boot·学习·算法·echarts·mybatis