注释规范
Java中的注释一般分为三种:文档注释、单行注释和多行注释。具体如下:
文档注释
/**
* Class that exposes the Spring Boot version. Fetches the
* {@link Name#IMPLEMENTATION_VERSION Implementation-Version} manifest attribute from the
* jar file via {@link Package#getImplementationVersion()}, falling back to locating the
* jar file that contains this class and reading the {@code Implementation-Version}
* attribute from its manifest.
* <p>
* This class might not be able to determine the Spring Boot version in all environments.
* Consider using a reflection-based check instead: For example, checking for the presence
* of a specific Spring Boot method that you intend to call.
*
* @author Drummond Dawson
* @author Hendrig Sellik
* @author Andy Wilkinson
* @since 1.3.0
*/
文档注释一般用于类注释、方法注释、字段注释。
单行注释
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
DefaultBootstrapContext bootstrapContext, ApplicationArguments applicationArguments) {
// Create and configure the environment
ConfigurableEnvironment environment = getOrCreateEnvironment();
configureEnvironment(environment, applicationArguments.getSourceArgs());
ConfigurationPropertySources.attach(environment);
listeners.environmentPrepared(bootstrapContext, environment);
DefaultPropertiesPropertySource.moveToEnd(environment);
Assert.state(!environment.containsProperty("spring.main.environment-prefix"),
"Environment prefix cannot be set via properties.");
bindToSpringApplication(environment);
if (!this.isCustomEnvironment) {
environment = convertEnvironment(environment);
}
ConfigurationPropertySources.attach(environment);
return environment;
}
单行注释一般用于方法内。对部分复杂逻辑进行说明。
多行注释
/*
* Copyright 2012-2022 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
*
* https://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.
*/
类中各种注释的位置
/*
* 这是文件注释,使用多行注释,一般用于说明源文件的版权信息
*/
package org.depsea.example;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* 这里是类注释,使用文档注释
*/
public class CommentExample {
/**
* 字段注释,使用文档注释
*/
private LocalDateTime dateTime;
/**
* 这里是方法注释,使用文档注释
*/
public void setDateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}
/**
* 这里是方法注释,使用文档注释
*/
public String getDateTimeStr() {
// 方法内的注释,使用单行注释
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return formatter.format(this.dateTime);
}
}