什么是package-info.java

简介: 什么是package-info.java

在java项目中创建package以后,可以在package下放一个package-info.java文件。

这个文件有什么作用?如何生成?

三个作用:

  1. 为标注在包上Annotation提供便利;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-B8NbPmPY-1676540895498)(null)]

  1. 声明友好类和包常量;

比如一个包中有很多的内部访问的类或常量,就可以统一的放到package-info类中,这样就方便,而且集中管理,减少friendly类到处游走的情况,看例子:

//这里是包类,声明一个包使用的公共类,强调的是包访问权限
class PkgClass{
    public void test(){
    }
}
//包常量,只运行包内访问,适用于分“包”开发
class PkgConst{
    static final String PACAKGE_CONST="ABC";
}
  1. 提供包的整体注释说明。
Package Documentation
Prior 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.

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tQX5zYAQ-1676540895900)(null)]

package-info.java’s purpose
The 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.
  1. 如何创建:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-81KZxIbt-1676540893796)(null)]

参考

What’s package-info.java for?

目录
相关文章
|
3月前
|
Java 数据安全/隐私保护
Java 包(package)的使用详解
Java中的包(`package`)用于组织类和接口,避免类名冲突并控制访问权限,提升代码的可维护性和可重用性。通过`package`关键字定义包,创建相应目录结构即可实现。包可通过`import`语句导入,支持导入具体类或整个包。Java提供多种访问权限修饰符(`public`、`protected`、`default`、`private`),以及丰富的标准库包(如`java.lang`、`java.util`等)。合理的包命名和使用对大型项目的开发至关重要。
187 2
|
4月前
|
Java Maven 数据库
|
6月前
Failed to bind properties under ‘logging.level‘ to java.util.Map java.lang.String, java.lang.String
Failed to bind properties under ‘logging.level‘ to java.util.Map java.lang.String, java.lang.String
44 0
|
Java
【异常】Failed to bind properties under ‘logging.level‘ to java.util.Map<java.lang.String, java.lang.Str
【异常】Failed to bind properties under ‘logging.level‘ to java.util.Map<java.lang.String, java.lang.Str
264 0
|
Java
SpingBoot2.x文件上传:failed to convert java.lang.String to org.springframework.util.unit.DataSize
SpingBoot2.x文件上传:failed to convert java.lang.String to org.springframework.util.unit.DataSize
411 0
|
存储 网络协议 Java
java基础 --- java包(package)
java基础 --- java包(package)
129 0
|
Java 测试技术
Cannot instantiate test(s): java.lang.SecurityException: Prohibited package name: java
Cannot instantiate test(s): java.lang.SecurityException: Prohibited package name: java
Cannot instantiate test(s): java.lang.SecurityException: Prohibited package name: java
com.google.common.base.Preconditions.checkNotNull(Preconditions.java:877)
com.google.common.base.Preconditions.checkNotNull(Preconditions.java:877)
91 0
|
IDE Java Maven
package-info.java
package-info.java 文件估计大家见过但是自己却很少去创建和使用它、因为对于一般应用来说可能真的太少见了。
407 0
|
Java 编译器
Java - package & import
Java - package & import
113 0
Java - package & import