In the process of looking at the spring source code, is there a source file such as package-info.java? What is it for?

Time:2021-9-21

When reading the spring framework source code, do you still have the impression that there will be a package-info.java file under each package? If you don’t remember, you can take another look and open it to see what’s inside. So what is this file for? Let’s talk briefly! WeChat search official account No.“The legend of rookie gods”, choose suitable learning materials! Continue to share the common technical dry goods of first-line large factories.

1、 Introduction

Package info.java is a java source code file, which can be put into any Java source code package. However, it cannot be created by creating ordinary Java classes, because there is a ‘-‘ in its file name, which does not comply with Java variable naming norms. However, some development tools, such as the commonly used idea, provide a method to directly create the package-info.java class. You can try it.

The content of this class is usually very simple, but it also has certain specifications. Its main function is to provide some package level operations, such as:Package level comments, package level comments, package level public variables

2、 Mode of use

2.1. Provide package level comments

When using Javadoc to generate a Java annotation document, the annotation description written on package-info.java will be generated into the document. For example, the package info.java file under the org.springframework.beans package in the spring beans module in the spring source code 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 content in the corresponding Javadoc: link:https://docs.spring.io/spring…
In the process of looking at the spring source code, is there a source file such as package-info.java? What is it for?
This method can give a brief description of the functions of an entire package.

2.2 package level annotation

There are also many package level annotations in the spring source code, but many annotations are nested and complex. For a simple example, suppose we provide an SDK to others, in which all interfaces under a package need to be upgraded. The old ones can’t be deleted directly, but can only be marked expired first. How can we mark them? In the old way, class by class annotation.

After the package level annotation is available, we will directly mark the annotation in package-info.java under the package, and all classes in the package will be marked as expired, as follows:
In the process of looking at the spring source code, is there a source file such as package-info.java? What is it for?
After doing so, after generating the Javadoc document, you can see a situation of the whole package in the Javadoc.

2.3 public variables at package level

If you want to provide constants or classes in a package that can only be used under this package, other packages cannot use them. It can be implemented directly through package-info.java.
Sample code
package-info.java

/**
 * @author wangbing
 */
package com.wb.spring.testpkg;

/**
 * @author wangbing
 *Package level classes
 */
class PckInner {
}
/**
 * @author wangbing
 *Package level constants
 */
class PckInnerConstant {
 public static final String MY_CONSTANT = "My_Constant";
}

Test1.java under the same package

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

Test2.java under other packages
In the process of looking at the spring source code, is there a source file such as package-info.java? What is it for?
When looking at some other source code, you will find that there are many package-info.java files. Although the function of this file is not so complex, it is still not harmful to learn more!

List of spring source code articles:https://segmentfault.com/a/11…