什麼是package-info
在java專案中建立package以後,可以在package下放一個package-info.java檔案。
這個檔案有什麼作用?如何生成?
三個作用:
-
為標註在包上Annotation提供便利;
package annotations
-
宣告友好類和包常量;
比如一個包中有很多的內部訪問的類或常量,就可以統一的放到package-info類中,這樣就方便,而且集中管理,減少friendly類到處遊走的情況,看例子:
//這裡是包類,宣告一個包使用的公共類,強調的是包訪問許可權 class PkgClass{ public void test(){ } } //包常量,只執行包內訪問,適用於分“包”開發 class PkgConst{ static final String PACAKGE_CONST="ABC"; }
- 提供包的整體註釋說明。
Package DocumentationPrior to Java 5, package level documentation (the documentation shown in Javadocs for a package) was placed in package.html. Today, the description and other related documentation for a package can be written up in the package-info.java file and it gets used in the production of the Javadocs. As a demonstration, the example package-info.java…
/** * Domain classes used to produce the JSON and XML output for the RESTful services. * <p> * These classes contain the JAXB annotations. * * @since 1.0 * @author jwhite * @version 1.1 */ package com.intertech.cms.domain;
… results in the following Javadocs.

package documentation
package-info.java’s purposeThe package-info.java is a Java file that can be added to any Java source package. Its purpose is to provide a home for package level documentation and package level annotations. Simply create the package-info.java file and add the package declaration that it relates to in the file. In fact, the only thing the package-info.java file must contain is the package declaration.
- 如何建立:

create package.java