Java 9中的开放模块是什么,如何使用? [英] What is an open module in Java 9 and how do I use it?

查看:105
本文介绍了Java 9中的开放模块是什么,如何使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

带有 open 关键字的模块与不带有 open 关键字的模块之间有什么区别?例如:

What is the difference between a module with the open keyword before it and without it? For instance:

open module foo {
}

module foo {
}

推荐答案

为了提供对模块的反射访问,Java 9引入了 open 关键字.

In order to provide reflective access to your module, Java 9 introduced the open keyword.

您可以使用模块声明中的 open 关键字来创建打开的模块.

You can create an open module by using the open keyword in the module declaration.

open 模块将其所有软件包的反射访问权限授予其他模块.

An open module grants reflective access to all of its packages to other modules.

例如,如果您要使用某些严重依赖反射的框架(例如Spring,Hibernate等),则可以使用此关键字为其启用反射访问.

For example, if you want to use some framework that heavily relies on reflection, such as Spring, Hibernate, etc, you can use this keyword to enable reflective access for it.

您可以使用包声明中的 opens 语句为模块的指定包启用反射访问:

You can enable reflective access for specified packages of your module by using the opens statement in the package declaration:

module foo {
    opens com.example.bar;
}

或通过在模块声明中使用 open 关键字:

or by using the open keyword in the module declaration:

open module foo {
}

但请记住,您不能将它们组合在一起:

but keep in mind, that you cannot combine them:

open module foo {
    opens com.example.bar;
}

导致编译时错误.

希望有帮助.

这篇关于Java 9中的开放模块是什么,如何使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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