package-info.java_package-info.java到底是什么

本文介绍了Java中的package-info.java文件的作用,它用于提供包级别的注释和文档。在Java 5之前,包级文档放在package.html中,而现在可以放入package-info.java中,并能生成Javadoc。此外,package-info.java还可用于存放包级注解,例如批量废弃包中的所有类型。了解这个文件可以帮助更好地组织和注解Java项目。
摘要由CSDN通过智能技术生成

发现距离上一次在这里写博客已经三个多月了。。。说好的笔耕不辍呢=.=

Anyway,今天(确切说是昨天晚上)在code review中被组里的QA II问到在一个叫做package-info.java的java文件中的一些字符串能不能定义成constant。这里先介绍一下这个叫做package-info.java的java文件的大概情况:有若干annotation,有package,有import,但是没有任何的class定义。我试了一下发现如果想要在这里定义一个class,因为class名中不允许有“-”,所以是行不通的,当然也可以在外面新建一个java class然后放上static final string,不过显然为了两三个只在一个package里面用到的string而搞一个java class有点overkill了。。。于是解释说好像不是个好主意,然后cr过了。上班时由于sprint有任务时间稍紧,所以也没太追究这个名字首字母小写且没有任何class定义的java file到底是何方神圣。

下班之后又意外发现在intellij里面如果右键new东西的话居然有个选项是new一个package-info.java。。。简直醉了,既然这么有缘分那咱就google一下看看你到底是神马玩意吧!

这一google才发现原来这玩意不是我司内部的玩意,在http://www.intertech.com/Blog/whats-package-info-java-for/ 中作者详细介绍了这个package-info.java到底是什么,干了什么,以及为什么。原文截取如下:

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

packagecom.intertech.services;

The package-info.java file above must sit in the com.intertech.services package.

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…

1 /**

2 * DOMAIN CLASSES USED TO PRODUCE THE JSON AND XML OUTPUT FOR THE RESTFUL SERVICES.3 *

4 * THESE CLASSES CONTAIN THE JAXB ANNOTATIONS.5 *6 * @SINCE 1.07 * @AUTHOR JWHITE8 * @VERSION 1.19 */

10 package com.intertech.cms.domain;

… results in the following Javadocs.

package-descrption.png

package-doc-480x396.png

Package Annotations

Perhaps more importantly to today’s annotation driven programmer, the package-info.java file contains package level annotations. An annotation with ElementType.PACKAGE as one of its targets is a package-level annotation and there are many of them.  Using your favorite IDE’s code assistant (shown in Eclipse below) in a package-info.java file and you will find a number package annotation options.

package-annotations-480x396.png

For example, perhaps you want to deprecate all the types in a package. You could annotate each individual type (the classes, interfaces, enums, etc. defined in their .java files) with @Deprecated (as shown below).

1 @Deprecated2 public class Contact{3 }

Or, you could use the @Deprecated on the package declaration in package-info.java.  This has the effect of deprecating everything in the package in one fell swoop.

@Deprecatedpackage com.intertech.cms.domain;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值