基于 SpringBoot 的快速开发框架

cz-core

基于 SpringBoot 的自研核心依赖,用于快速开发项目

名称版本
SpringBoot2.0.3

0、依赖本 jar 包

在新建好的项目中,加入以下依赖:

        <dependency>
            <groupId>top.changelife.zero</groupId>
            <artifactId>cz-core</artifactId>
            <version>1.0.0</version>
        </dependency>

如果需要连接数据库的项目,加入以下依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>2.0.1.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

至此,项目已经可以跑起来。

1、配置启动器

本处所说的配置,均指 SpringBoot 项目的配置。

启动器默认是开启的,效果是会执行 top.changelife.zero.core.init.StartExecuter 类中的 start 方法。

start 方法中,会搜寻项目中所有实现了接口 top.changelife.zero.core.init.InitService 的类,调用里面的 init 方法,主要的初始化代码,都可以编写在 init 方法里面。

此时启动器还无法在项目启动时执行,还需要最后一步,就是在启动类 XxxApplication 上加一个注解 @EnableZeroCore,至此配置完毕。

如果需要关系启动器的话,在配置文件中加入 custom.init.enable=false 即可。

配置文件的话,application.propertiesapplication.yml 都可以,如果你想用 bootstrap 的话,那么我想你应该需要引入 spring-cloud-context 等依赖,这些就超范围了,在此不再赘述。

2、配置数据库

2.1、推荐的方式是使用阿里开源的 Druid,配置方式如下:

custom.datasource.type=com.alibaba.druid.pool.DruidDataSource
custom.datasource.url=jdbc:mysql://localhost:3306/test
custom.datasource.username=root
custom.datasource.password=123456
custom.datasource.driverClassName=com.mysql.jdbc.Driver

项目启动后可访问 http://localhost:8080/driud 查看提供的监控页面,默认账号密码是 name:password

2.2、如果你不喜欢我推荐的数据源,那么你可以使用 SpringBoot 自带的 HikariDataSource,配置方式如下:

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.jdbc.Driver

2.3、如果你想自定义的话,就会麻烦一点,可以加个配置类,代码如下:

@Configuration
public class DataSourceConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.a")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }
}

那么你就需要这么配置:

spring.datasource.a.jdbc-url=jdbc:mysql://localhost:3306/test
spring.datasource.a.username=root
spring.datasource.a.password=123456
spring.datasource.a.driverClassName=com.mysql.jdbc.Driver

提示:如果三种方式你都配置了,那么生效的顺序会是 2.1、2.3、2.2。

3、接口开发

controller 包下需要新建 package-info.java 文件,代码如下:

@CustomModule("测试模块")
package com.test.controller;
import top.changelife.zero.core.annotation.CustomModule;

然后新建一个 TestController.java,代码如下:

@CustomController(value = "/test", name = "测试接口test")
public class TestController {
    private Logger logger = LoggerFactory.getLogger(TestController.class);
    @CustomMapping(name = "测试接口method")
    public Object test() {
        logger.info("test");
        return "test";
    }
}

4、全局日志控制

4.1、配置日志级别:custom.log.level=info

4.2、启用日志配置:custom.log.enable=true

4.3、实现接口:

public interface LogService {
    public void handlerLog(LogInfo logInfo);
}

如果需要将日志信息写库,或者需要查询一些当前请求用户的信息,然后与 LogInfo 里面的信息包装在一起,那么建议使用 @Async 注解,让操作的方法异步执行,不要影响接口返回。

@EnableAsync 注解已经加载日志的配置上,所以配置了 4.2 即可直接使用 @Async 注解。

5、文件 Api

5.1、启用文件配置:custom.file.type,可设置 localftpfastdfs

5.2、配置类:

@ConfigurationProperties(prefix = "custom.file")
public class FileProperties {
    /**
     * 通用部分
     */
    private String type;//FileService 的实现类的全类名
    private String encoding;//编码类型
    private String requestPrefix;//请求时的前缀
    private String nginxIp;//如果需要通过nginx直接访问静态资源,需要设置此IP
    private String nginxPort;//如果需要通过nginx直接访问静态资源,需要设置此端口
    /**
     * local:存本地时用
     */
    private String localDir;//本地地址
    /**
     * ftp:上传到ftp服务器时用
     */
    private String ftpHost;
    private int ftpPort;
    private String ftpUsername;
    private String ftpPassword;
    private boolean ftpPassiveMode;
    private int ftpConnectTimeout;
    private String ftpRootPath;
    /**
     * fastdfs:上传到fastdfs分布式文件系统时用
     */
    private String fastdfsConfigFileName;
}

如果是存本地,除了通用部分,你还需要配置 custom.file.localDir,设置文件本地存储的路径。

5.3、注入文件操作对象

@Autowired
FileService fileService;

5.4、自定义文件操作对象

继承 AbstractFileService 抽象类,配置 custom.file.type=xx.xxx.xx.MyFileService,此时注入进来的就是自定义的文件操作对象。

public class MyFileService extends AbstractFileService {
    @Override
    public String save(InputStream inputStream, String originalName) throws IOException {
        return null;
    }
}

此时文件操作对象中,已有 FileProperties 配置类,可以在文件操作的时候直接使用里面的属性。

如果需要新增一些属性,那么需要继承 FileProperties 类,然后在里面写上自己新加的属性。

注意前缀已经固定是 custom.file,此时假设属性设置为 name,那么就需要配置 custom.file.name

@Component
public class MyFileProperties extends FileProperties {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

6、定时任务

6.1、启用定时任务:custom.quartz.enable=true

6.2、定时任务对象:custom.quartz.beanNameList=obj1、obj2、obj3...

6.3、定时任务方法:

custom.quartz.beanMethodMap.obj1=method1,method2,method3
custom.quartz.beanMethodMap.obj2=method1,method2,method3

6.4、实例代码

@Component("obj1")
public class InvokeMethod {
    int i = 0;

    @CustomSchedule(value = "*/1 * * * * ?", misfirePolicy = 0, concurrent = false)
    public void method1() {
        try {
            Thread.sleep(1000);
        } catch (Exception e) {

        }
        System.out.println(i++ + "-------------test1");
    }

    @CustomSchedule(value = "*/1 * * * * ?", misfirePolicy = 0, concurrent = false)
    public void method2() {
        System.out.println(i++ + "-------------test2");
    }

    @CustomSchedule(value = "*/9 * * * * ?", misfirePolicy = 2, status = 1)
    public void method3() {
        System.out.println("test3");
    }

    @CustomSchedule(value = "*/9 * * * * ?", misfirePolicy = -1, status = 1)
    public void method4() {
        System.out.println("test4");
    }
}

6.5、@CustomSchedule 注解

value:cron 表达式,自定百度

misfirePolicy:定时任务错过处理方式

  • 0 默认(Quartz里面叫Smart,具体哪种我不太清楚)
  • 1 以当前时间为触发频率立刻触发一次执行,然后按照Cron频率依次执行
  • 2 不触发立即执行,等待下次Cron触发频率到达时刻开始按照Cron频率依次执行
  • 3 以错过的第一个频率时间立刻开始执行,重做错过的所有频率周期后,当下一次触发频率发生时间大于当前时间后,再按照正常的Cron频率依次执行

concurrent:是否支持并发执行

status:0 正常,1 暂定

6.6、QuartzController 实时操作定时任务

请求方式 POST,参数 jobName:

  • 暂停 /quartz/pause
  • 恢复 /quartz/resume
  • 删除 /quartz/delete
  • 触发 /quartz/trigger

触发就是立即执行一次,删除以后,就不能再触发或者恢复了。参数 jobName 是系统按照默认规则实现的,格式是 job-beanName-methodName,暂不支持自定义。

7、对象池使用

其实前面提供的 FileService 接口的实现类,ftp 和 fastdfs 都有使用对象池,因为频繁打开关闭连接太耗时,这里就单独讲解一下对象池的使用。

关键类:

  • CustomObjectPool 对象池本身
  • ? implements CustomPooledObjectFactory 池化对象的工厂接口
  • ? implements CustomPooledObject 池化对象的包装类

实例代码:

池化对象

public class TestPooledObject implements CustomPooledObject {
    private int age;
    private String name;

    public TestPooledObject() {
    }

    public TestPooledObject(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public void destroy() {
    }

    @Override
    public boolean validate() {
        return ObjectUtil.isNotNull(this);
    }

    @Override
    public <T> T get() {
        return (T)this;
    }
}

工厂实现

@Component
public class TestObjectFactory implements CustomPooledObjectFactory<CustomPooledObject> {
    private int i;

    @Override
    public CustomPooledObject create() {
        TestPooledObject testPooledObject = new TestPooledObject(i++, "cz");
        return testPooledObject;
    }
}

使用

@Autowired
TestObjectFactory testObjectFactory;

CustomObjectPool customObjectPool = new CustomObjectPool(testObjectFactory);
CustomPooledObject customPooledObject = customObjectPool.borrowObject();//从对象池中获取一个对象
TestPooledObject testPooledObject = customPooledObject.get();
//具体使用对象的逻辑写在这里
customObjectPool.returnObject(customPooledObject);//归还一个对象到对象池汇总

N、其他功能

  • 统一请求响应

  • 全局异常捕捉

  • 线程局部变量操作

  • WebSocket配置

  • Spring 时间异步处理配置

  • Spring Schedule 定时任务异步执行配置

  • 全局跨域配置

  • 工具类


至此,框架介绍完毕,可以开始愉快地编写代码了!!!


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值