上一篇工程可以初步运行了,这一篇目标是可以获取属性文件中的自定义值。

之前项目是rootProject包含moduleProject,moduleProject包含businessProject;businessProject是真正的逻辑实现工程,moduleProject只是用作功能分组文件夹使用,这种模式大家比较麻烦,于是吧moduleProject改成文件夹,选中rootProject创建businessProject,然后移动到对应文件夹下,初步效果如下:

模块

子模块

说明

nuts.springboot.boot

 

工程启动引导项目

endpoints

 

后端api响应接口

 

nuts-springboot-clover

模板项目后端

 

nuts-springboot-home

统一异常处理、统一返回结构

kernels

 

工程内核模型

 

nuts-springboot-core

核心模型

 

 

 

 

 

 

 

 

 

android 可以用spring springboot androidstudio_jar

这里需要注意的是,调整rootProject中的setting.gradle

rootProject.name = 'nuts.springboot.single'

include 'nuts-springboot-boot'
findProject(':nuts-springboot-boot')?.name = 'nuts.springboot.boot'

include 'components:nuts-springboot-logback'
findProject(':components:nuts-springboot-logback')?.name = 'nuts.springboot.logback'

include 'components:nuts-springboot-log4j2'
findProject(':components:nuts-springboot-log4j2')?.name = 'nuts.springboot.log4j2'

include 'endpoints:nuts.springboot.clover'
findProject(':endpoints:nuts-springboot-clover')?.name = 'nuts.springboot.clover'

include 'endpoints:nuts-springboot-home'
findProject(':endpoints:nuts-springboot-home')?.name = 'nuts.springboot.home'

include 'endpoints:nuts-springboot-auth'
findProject(':endpoints:nuts-springboot-auth')?.name = 'nuts.springboot.auth'

include 'kernels:nuts-springboot-core'
findProject(':kernels:nuts-springboot-core')?.name = 'nuts.springboot.core'

/*
** Modify the associated name of the setting.gradle file
*/
def modifyProjectBuildFileName(ProjectDescriptor project) {
    def fileName = project.name.substring(project.name.lastIndexOf('.') + 1) + ".gradle"

    logger.info String.format("%s Change the name of the build.gradle file of the %s module to %s.", new Date().getTimeString(), project.name, fileName)
    project.setBuildFileName(fileName)

    project.children.each {
        child ->
            modifyProjectBuildFileName(child)
    }
}
/*
** This code must be executed last,
**  otherwise the newly included file cannot modify the name mapping
*/
rootProject.children.each {
    project ->
        modifyProjectBuildFileName(project)
}

nuts-springboot-clover,新建HelloWorldController

android 可以用spring springboot androidstudio_spring boot_02

@RestController
@RequestMapping("/clover")
public class HelloWorldController {

    @RequestMapping("/sayHi")
    public String sayHello() {
        return "Hello World!";
    }
}

 然后在nuts-springboot-boot项目中引入nuts-springboot-clover

implementation project(':endpoints:nuts.springboot.clover')

然后启动项目

android 可以用spring springboot androidstudio_android 可以用spring_03

注意:

nuts-springboot-clover的 clover.gradle文件中需要加入以下内容:

bootJar { enabled = false } jar { enabled = true }

这主要解决编译过程报无法找到符号错误。

个人理解就是使用springboot框架及其插件,默认生成可执行的jar(无法被当做类库jar使用);所以需要手动指明,生成类库jar,而不是生成可执行的jar。

在使用gradle编译运行中文提示可能乱码,可以在【Help】-》【Edit Custom VM Options...】中加入以下语句:

-Dfile.encoding=UTF-8

重启Idea即可解决中文乱码。

  • springboot属性配置yaml文件

随着组件,功能的不断增加,配置文件也不断的膨胀;项目越大,配置文件项目,越多越混乱,最终好多自定义属性都不清楚是在哪里使用的。鉴于历史经验,属性分散在具体module项目中的就近配置原则,并且boot只配置端口,url地址头等web容器相关的属性,clover是后端功能集成项目,这里主要包括使用了哪些项目和引入相关配置文件。

boot项目yaml文件

android 可以用spring springboot androidstudio_spring_04

clover项目yaml文件,其中自定义项以[module][.功能][.xxx][.xxx]形式定义。

android 可以用spring springboot androidstudio_jar_05

读取属性配置值:

android 可以用spring springboot androidstudio_spring boot_06

android 可以用spring springboot androidstudio_gradle_07

nuts-springboot-home,增加GlobalExceptionHandler 类,通过@ExceptionHandler响应未特别指定的Exception的通用处理逻辑:

android 可以用spring springboot androidstudio_spring boot_08

@RestController 标注的controller 默认返回对象都是以json格式返回给前台,为了统一,定制统一结构如下:

 

android 可以用spring springboot androidstudio_android 可以用spring_09

clover项目中的HelloWorldController类修改为:

@RestController
@RequestMapping("/clover")
public class HelloWorldController {

    @RequestMapping("/sayHi")
    public String sayHello() {
        return "Hello World!";
    }

    @Autowired
    private Student student;

    @RequestMapping("/info")
    public Student getInfo() {
        return student;
    }

    @RequestMapping("/err")
    public RResponse getError() {
        if (new Random().nextInt() % 2 == 0)
            throw new NullPointerException("This is Test.");

        return RResponse.ofSuccess(student);
    }

}

boot项目yaml 引用 clover 项目,clover项目引用 home项目yaml

运行效果:

android 可以用spring springboot androidstudio_android 可以用spring_10

android 可以用spring springboot androidstudio_gradle_11