Spring编码规范

阿里云服务器优惠券领取  优惠券

该文档定义了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 进行格式化。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值