Did you remember from reading the Spring Framework source code that there is a package-info.java file under every package? If you don’t remember, take a second look, open it up and see what’s inside. So what is this file for? Here is a simple chat! WeChat Search Official Account:
“The Rookie’s Seal of the Gods”, choose a variety of learning materials for their own! Continue to share common technology dry goods of front-line big factories.

Introduce a,

Package-info.java is first a Java source file that can be placed in any Java source package. However, it cannot be created by creating a normal Java class because it has a ‘-‘ in the file name, which does not conform to Java’s variable naming convention. However, some development tools, such as the popular IDEA, provide ways to create package-info.java classes directly, which you can try.

For this class, the content is usually very simple, but there are some specifications, and the main purpose is to provide some packet-level operations, such as packet-level comments, packet-level comments, packet-level public variables.

Two, the way of use

2.1. Provide package-level comments

When Java annotation documents are generated using Javadoc, the annotation annotations written above package-info.java are typically generated into the document. Such as spring – beans in the spring source module of org. Springframework. Beans bag package – info. Under the Java file, the content is as follows:

/**
 * This package contains interfaces and classes for manipulating Java beans.
 * It is used by most other Spring packages.
 */
@NonNullApi
@NonNullFields
package org.springframework.beans;

import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;

Take a look at the contents of the corresponding Javadoc:https://docs.spring.io/spring…



This provides a brief description of the functionality of an entire package.

2.2 Package-level annotations

There are also many package-level annotations in the Spring source code, but many of them are nested and complex. For example, suppose we provide an SDK to others, and all the interfaces under one package need to be upgraded. The old ones cannot be deleted directly, but can only be marked as expired first. Then how can we mark them? In the old way, class by class annotation.

If we have a package-level annotation, we annotate the annotation directly in package-info.java below the package, and all classes in the package will be marked as expired, as follows:



After you do this, you can see a picture of the entire package in Javadoc after generating the Javadoc documentation.

2.3 Package-level public variables

If you want to provide constants or classes in a package that are only available under that package, no other package can use them. This can be implemented directly through package-info.java. The sample code package-info.java

/** * @author wangbing */ package com.wb.spring.testpkg; /** * @author wangbing * class PckInner {} /** * @author wangbing * package level constant */ class pckinnerConstant {public static final String MY_CONSTANT = "My_Constant"; }

Test1.java under the same package

Public class Test1 {public static void main(String[] args) {/** * @author wangbing */ public class Test1 {public static void main(String[] args) {// Pkinner inner = new PckInner(); String myConstant = PckInnerConstant.MY_CONSTANT; }}

Test2.java under other packages



When looking at some of the other source code, you will find that there are also a lot of package-info.java files, the role of this file is not so complex, but after all, learn a little more, always there is no harm!

The Spring source articles list: https://segmentfault.com/a/11…