博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
package-info.java——简介
阅读量:6670 次
发布时间:2019-06-25

本文共 2982 字,大约阅读时间需要 9 分钟。

hot3.png

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.

/** *    Copyright 2009-2015 the original author or authors. * *    Licensed under the Apache License, Version 2.0 (the "License"); *    you may not use this file except in compliance with the License. *    You may obtain a copy of the License at * *       http://www.apache.org/licenses/LICENSE-2.0 * *    Unless required by applicable law or agreed to in writing, software *    distributed under the License is distributed on an "AS IS" BASIS, *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *    See the License for the specific language governing permissions and *    limitations under the License. *//** * Parsing utils */package org.apache.ibatis.parsing;

在Mybatis源码中无意中看到的

输入图片说明

package-info.java的作用

主要作用为:

  • 为标注在包上Annotation提供使用
  • 声明包的私有类和常量
  • 提供包的整体注释说明

Annotation

//MyAnnotation.java@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.PACKAGE)public @interface MyAnnotation {    }//package-info.java/** *  我是一个包 -_- */@MyAnnotationpackage com.demo;// Test.javapublic class Test {    public static void main(String[] args) {        String packageName = "com.demo";        Package pkg = Package.getPackage(packageName);        Annotation[] annotations = pkg.getAnnotations();        for (Annotation annotation : annotations) {            if (annotation instanceof MyAnnotation){                System.out.println("MyAnnotation");            }        }    }}

声明包的私有类和常量

可以创建一些只有在这个包调用的私有类或者常量,其他包是不能访问的。

//package-info.javapackage com.demo;class MyClass{    void test() {        System.out.println("MyClass.test()");    }}class MyConst {    static final String str = "MyConst";}// Test.javapublic class Test {    public static void main(String[] args) {        MyClass myClass = new MyClass();        myClass.test();        System.out.println(MyConst.str);    }}

提供包的整体注释说明

/** *    Copyright 2009-2015 the original author or authors. * *    Licensed under the Apache License, Version 2.0 (the "License"); *    you may not use this file except in compliance with the License. *    You may obtain a copy of the License at * *       http://www.apache.org/licenses/LICENSE-2.0 * *    Unless required by applicable law or agreed to in writing, software *    distributed under the License is distributed on an "AS IS" BASIS, *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *    See the License for the specific language governing permissions and *    limitations under the License. *//** * Parsing utils */package org.apache.ibatis.parsing;

给这个包整体的注释说明 ,在生成JavaDoc文档时,会显示这些包注释。

输入图片说明

转载于:https://my.oschina.net/isaac21/blog/1621055

你可能感兴趣的文章
开始nodejs+express的学习+实践(5)
查看>>
在微信小游戏中开发一个贪食蛇
查看>>
通过class获取data-id以及相应的对象
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
SQLServer 2008数据库计算机名更改
查看>>
Tkinter的消息对话框
查看>>
windows 远程桌面kali
查看>>
我的友情链接
查看>>
常见linux系统中RPM包的通用命名规则
查看>>
DELL LATITUDE E6320等笔记本不能识别移动硬盘
查看>>
Debian6.0.7的archive mirror列表真接地气
查看>>
让自己的程序支持CD刻录功能
查看>>
elasticsearch使用java api批量插入数据
查看>>
Linux系统新手学习的11点建议
查看>>
python开发支持万台设备的分布式监控软件视频教程
查看>>
手机上的html
查看>>
Spring整合Shiro做权限控制模块详细案例分析
查看>>
学号Linux的好帮手
查看>>
为 Asp.net 网站新增发送手机短信功能
查看>>