阿里云服务器优惠券领取 优惠券
该文档定义了Spring Framework中源文件的编码标准。 它主要供Spring Framework团队使用,但可供贡献者用作参考。
本文档的结构基于Google Java样式参考,目前正在进行中。-摘自https://github.com/spring-projects/spring-framework/wiki/Code-Style
1. 不遵循Spring编码规范的调试法
修改spring-framework/src/checkstyle/checkstyle.xml,删除所有检查项,仅保留最外层<module></module>标签。
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN" "https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="com.puppycrawl.tools.checkstyle.Checker">
</module>
然后就可以以自己的风格编写调试代码并成功编译了。
2. 遵循Spring编码规范的调试法
2.1 google style
https://github.com/google/styleguide
2.2 IDEA安装google style
1. 下载intellij-java-google-style.xml
https://github.com/google/styleguide/blob/gh-pages/intellij-java-google-style.xml
2. 导入intellij-java-google-style.xml
3. 按google style格式化代码
2.3 spring style
2.3.1 官方文档
https://github.com/spring-projects/spring-framework/wiki/Code-Style
2.3.2 IDEA File And Code Templates设置
1. 设置Class
行首需要顶格,需留有一个空的末行,并且末行行首不能留有空格。
#parse("File Header.java")
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
/**
* description class ${NAME}.
*
* @author Lazy Hu
*/
public class ${NAME} {
}
2. 设置File Header
行首需要顶格,需留有一个空的末行,并且末行行首不能留有空格。
/*
* Copyright 2002-2019 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.
*/
3. 设置Junit Test Class
行首需要顶格,需留有一个空的末行,并且末行行首不能留有空格。
#parse("File Header.java")
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
/**
* Unit tests for {@link ${NAME}s}.
*
* @author Lazy Hu
*/
class ${NAME}s {
${BODY}
}
4. 设置Spring
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
5. 设置try catch finally格式
6. 设置getter模板
按如上方式设置以后,用IDEA创建新文件,就会按Spring的编码规范生成文件。编写好源码以后,使用google code style 进行格式化。