什么是Java 9中的开放模块以及如何使用它 [英] What is an open module in Java 9 and how to use it

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

问题描述

模块与 open 关键字之前和之后有什么区别?

例如:

What is the difference between module with open keyword before 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 open keyword.

您可以在模块声明中使用 open 关键字创建开放模块。

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

open 模块授予对其他模块的所有包的反射访问权。

An open module grants reflective access to all of it's 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 opens statement in package declaration:

module foo {
    opens com.example.bar;
}

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

or using open keyword in 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天全站免登陆