简体   繁体   English

在Play Framework中混合Scala和Java

[英]Mixing scala and java in Play Framework

I have a Java file that looks like this: 我有一个如下所示的Java文件:

package AuthorizeNetFingerprint;


class Fingerprint {
    private static Log logger = LogFactory.getLog(Fingerprint.class);

    private long sequence;
    private long timeStamp;
    private String fingerprintHash;

    private Fingerprint() {
    }

    /**
     * Creates a fingerprint with raw data fields.
     * 
     * @param loginID
     * @param transactionKey
     * @param sequence : this number will be concatenated with a random value
     * @param amount
     * @return A Fingerprint object.
     */
    public static String createFingerprint(String loginID,
                    String transactionKey, long sequence, String amount) {
         return transactionKey;
    }
}

And I am trying to access it like this scala: 我正在尝试像这样的scala来访问它:

val fingerprint = new AuthorizeNetFingerprint.Fingerprint
val x_fp_hash = fingerprint.createFingerprint(x_api_login_id,
                  transaction_key, x_fp_sequence, x_amount)

And it gives me this error: 它给了我这个错误:

object Fingerprint in package AuthorizeNetFingerprint cannot be accessed in package AuthorizeNetFingerprint 包AuthorizeNetFingerprint中的对象指纹不能在包AuthorizeNetFingerprint中访问

Is it possible to mix scala and java in Play Framework? 是否可以在Play Framework中混合使用Scala和Java?

What I am doing wrong? 我做错了什么?

EDIT 编辑

I needed: public class Fingerprint 我需要:公共课指纹

instead of 代替

class Fingerprint

Three things: 三件事:

  1. As you've already figured out, your Fingerprint class needs to be public. 正如您已经知道的那样,您的Fingerprint类需要公开。
  2. You've made Fingerprint's constructor private; 您已经将Fingerprint的构造函数设为私有; you can't instantiate it. 您无法实例化它。
  3. Any static methods in a Java class should be accessed through the class' companion object in Scala. Java类中的任何静态方法都应通过Scala中该类的伴随对象进行访问。

All the Scala code in your example should be replaced by: 您的示例中的所有Scala代码都应替换为:

val x_fp_hash = AuthorizeNetFingerprint.Fingerprint.createFingerprint(…)

This works in the Scala (2.9.1) console, compiled with sbt (0.11.3). 这在使用sbt(0.11.3)编译的Scala(2.9.1)控制台中有效。

Yes, you can mix Java and Scala in a Play2 application, just put the Java code in the app directory. 是的,您可以在Play2应用程序中混合使用Java和Scala,只需将Java代码放入应用程序目录即可。 Note that Java classes need to be in their corresponding package directories, which is not the case for Scala classes. 请注意,Java类必须位于其相应的包目录中,而Scala类不是这种情况。

I am not familiar with the Play framework, but the first line in your Scala sample code should instantiate the class AuthorizeNetFingerprint.Fingerprint , which only has a private constructor and is not a public class (ie, it can only be accessed from the same package). 我不熟悉Play框架,但是Scala示例代码的第一行应实例化类AuthorizeNetFingerprint.Fingerprint ,该类仅具有私有构造函数 ,而不是公共类 (即,只能从同一包中访问)。

Maybe a call to AuthorizeNetFingerprint.Fingerprint.createFingerprint(...) works, after making the class public ? public授课后,也许对AuthorizeNetFingerprint.Fingerprint.createFingerprint(...)的调用起作用了?

In theory this should work just fine. 从理论上讲,这应该很好。 Check out this blog Re Java and Scala interop. 查看此博客Re Java和Scala互操作。 http://www.codecommit.com/blog/java/interop-between-java-and-scala http://www.codecommit.com/blog/java/interop-between-java-and-scala

When creating the project did you specify Java, Scala or None (1,2 or 3) 在创建项目时,您是否指定了Java,Scala或None(1、2或3)

It's conventional to name Java packages in all lower case. 通常以小写形式命名Java软件包。

It also appears that Scala gets confused if you try to use a Java package that starts with upper case. 如果您尝试使用以大写开头的Java包,Scala也会感到困惑。 If you use authorize as the package name instead of AuthorizeNetFingerprint , it will compile. 如果使用authorize而不是AuthorizeNetFingerprint作为包名,它将进行编译。

Also, there's no need for this: 另外,也不需要这样做:

val fingerprint = new AuthorizeNetFingerprint.Fingerprint 

createFingerprint is a static method, so just call createFingerprint是静态方法,因此只需调用

val x_fp_hash = Fingerprint.createFingerprint

(after importing authorize.Fingerprint ). (在导入authorize.Fingerprint )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM