java - 将 Java 和 Scala 集成到一个项目中

标签 java scala

我非常熟悉 Java 及其 Spring 框架。 所以我们正在使用 Spring Boot 项目,我们已经使用 java 实现了各种服务,现在我们想在其中添加一些 scala 服务(用于 Spark 实现)。

这样我就可以轻松处理来自 Controller 的请求(由 UI 提交)。

任何人都可以建议我可以遵循的一些不同方法或最佳实践来构建项目和创建服务

如果您能用一些示例或用例进行解释,将会很有帮助。

最佳答案

我经常使用的混合 Java 和 Scala 的方法是基于 Maven 的。 将 Scala 编译器插件放在 pom.xml 中的 Maven 编译器插件之上:

    <sourceDirectory>src/main/java</sourceDirectory>
    <!--<testSourceDirectory>src/test/scala</testSourceDirectory>-->
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.4.1</version>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <configuration>
                <recompileMode>incremental</recompileMode>
            </configuration>
            <executions>
                <execution>
                    <id>scala-compile-first</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>add-source</goal>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <args>
                            <arg>-Ydelambdafy:method</arg>
                            <arg>-target:jvm-1.8</arg>
                            <arg>-deprecation</arg>
                            <arg>-feature</arg>
                            <arg>-unchecked</arg>
                            <arg>-language:implicitConversions</arg>
                            <arg>-language:postfixOps</arg>
                        </args>
                    </configuration>
                </execution>
                <execution>
                    <id>scala-test-compile</id>
                    <phase>process-test-resources</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                    <configuration>
                        <args>
                            <arg>-Ydelambdafy:method</arg>
                            <arg>-target:jvm-1.8</arg>
                            <arg>-deprecation</arg>
                            <arg>-feature</arg>
                            <arg>-unchecked</arg>
                            <arg>-language:implicitConversions</arg>
                            <arg>-language:postfixOps</arg>
                        </args>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <compilerId>javac</compilerId>
                <debug>true</debug>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

我创建了a sample project at Github .

关于java - 将 Java 和 Scala 集成到一个项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51042738/

相关文章:

java - Scala:字符串 "+"与 "++"

scala - Gatling - 按顺序执行场景

Java JodaTime 期间格式化

java - Play 的 Java 反射的性能和替代方案!框架类路由功能

java - 如何在时间序列图表上显示数据库中存储的时间

java - 在 ListView 中显示数据库

json - 将 Akka actorRef 发送到 json

java - 模块名和目录名冲突?

scala - 在scala varargs中强制单个参数

scala - 在 intellij 引用我在我使用过的类中实际没有使用的 scala 方法时出现错误