Scala和Java在同一项目中 [英] Scala and Java in same project

查看:625
本文介绍了Scala和Java在同一项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在同一项目中使用Scala和Java吗?我是编程的新手,所以这对我来说有点令人困惑.

从我的研究中,我已经读到,获得一个好的项目的最佳组合是Java/Spring或Scala/Lift的组合.为什么存在这些组合?无法在Spring中使用Scala或在Lift中使用Java?

如果我使用Scala,是否应该在Scala代码中拥有整个配置?还是我可以拥有外部资源,所以我不应该每次都在Scala代码中进行修改.

解决方案

.我可以在同一项目中使用Scala和Java吗?

当然可以.您可以在同一项目中一起使用大多数基于JVM的语言.

.我该怎么做?

基本思想是配置您的构建工具以同时编译Java和Scala代码.例如,如果您使用Maven,则可以将 Maven Scala插件用于编译Scala代码.使用此插件有两种选择:

  1. 禁用 Maven编译器插件,该插件默认情况下会编译Java代码.然后,让Maven Scala插件同时编译Java和Scala代码.
  2. 继续使用Maven编译器插件编译Java.将Maven Scala插件配置为仅编译Scala代码.

我更喜欢选项2,因为我发现Scala编译非常慢(尽管每个新的Scala版本性能一直在稳步提高),因此使用Maven Compiler插件可以减少总体构建时间.

问.无法在Spring中使用Scala或在Lift中使用Java?

确定您可以将Scala与Spring结合使用,并将Java与Lift结合使用.您可以检查使用Java,Scala和Spring的我的示例应用程序. /p>

您会注意到,示例应用程序具有用于Java和Scala代码的单独的源文件夹,尽管这不是必需的,并且可以在Maven配置中进行配置.

.如果我使用Scala,是否应该在Scala代码中包含整个配置?

我假设您的意思是Spring配置.是否要使用XML文件,Java类或Scala类配置Spring,完全取决于您.所有方法都行得通.这取决于您对哪种样式更满意和喜欢.

您甚至可以将配置分布在多个类中,甚至可以将XML和类组合起来.

Can I use Scala and Java in the same project? I am new with programming so it's a litte bit confusing for me.

From my research I have read that the best combination to get a good project is the combination Java/Spring or Scala/Lift. Why do these combinations exist? Is it not possible to use Scala with Spring or Java with Lift?

In case I use Scala, should I have the whole configuration in Scala code? Or can I have external resources so I shouldn't modify every time the Scala code.

解决方案

Q. Can I use Scala and Java in the same project?

Sure, you can. You can use most of the JVM based languages together in the same project.

Q. How do I go about it?

The basic idea is to configure your build tool to compile both the Java and the Scala code. For example, if you use Maven, you can use the Maven Scala plugin for compiling Scala code. There are two options for using this plugin:

  1. Disable Maven Compiler plugin that compiles Java code by default. Then, let the Maven Scala plugin compile both the Java and Scala code.
  2. Continue using the Maven Compiler plugin to compile Java. Configure the Maven Scala plugin to compile only Scala code.

I prefer option 2 since I have found Scala compilation to be quite slow (although the performance has been improving steadily with each new Scala version) so using the Maven Compiler plugin reduces the overall build time.

Q. Is it not possible to use Scala with Spring or Java with Lift?

Sure you can use Scala with Spring and Java with Lift. You can check my sample application that uses Java, Scala and Spring.

You will notice that the sample application has separate source folders for Java and Scala code, although that is not mandatory and can be configured in the Maven configuration.

Q. In case I use Scala, should I have the whole configuration in Scala code?

I am assuming you mean Spring configuration. It is entirely up to you whether you wish to configure Spring using XML files, Java classes or Scala classes. All approaches work. It depends on which style you are more comfortable with and prefer.

You can even spread the configuration across multiple classes or even a combination of XML and classes.

这篇关于Scala和Java在同一项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆