经过前面的介绍,可以看到针对不同领域,都可以建立一个独立的组件。
有些组件是独立存在的,不依赖任何其他组件。比如状态机组件。
有些组件之间其实是需要互相配合,才能产生一定的作用。对此,组件的开发需要遵循一定的标准。框架也需要对组件进行有效管理。
组件依赖
组件依赖原则,需要遵循最小化原则。即
starter类型的组件只能依赖util类型的组件;
module类型的组件只能依赖util和starter类型的组件,而不能反过来
最小组件集
为了便于业务使用,我们定义个一个最小组件集,用来将大家经常用的组件集成在一起,形成一个便捷使用的脚手架。在此将之命名为:frame-bootstrap。中文为:框架启动组件
启动组件主要建设步骤如下:
1.定义pom
frame-bootstrap的核心就是定义一个pom,然后将基础性的组件依赖进去即可。
比如:
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- inner -->
<dependency>
<groupId>com.test</groupId>
<artifactId>frame-util-common</artifactId>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>frame-base</artifactId>
</dependency>
<!-- inner starter-->
<dependency>
<groupId>com.test</groupId>
<artifactId>frame-orm-starter</artifactId>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>frame-feign-starter</artifactId>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>frame-cache-starter</artifactId>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>frame-job-starter</artifactId>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>frame-request-starter</artifactId>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>frame-async-starter</artifactId>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>frame-config-starter</artifactId>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<!-- external -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
更重要的一点,别忘了使用框架的parent:
<parent>
<groupId>com.test</groupId>
<artifactId>frame-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath/>
</parent>
2.加入其他公共配置
还记得笔者之前提到的统一时区配置的问题吗?其实也可以放在这个组件下:
/**
* 全局配置时区
*/
@Configuration
public class GlobalTimeZoneConfig {
@PostConstruct
public void init(){
TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
}
}
当然,我们要保持此脚手架尽可能地简洁。
以上,我们详细介绍了各种组件的开发过程。
组件如何发布呢?
对每个组件,进行mvn deploy即可。当然,我们也可以定制自己的发布平台,进行自动化发布。
那如何接入框架呢?
很简单,pom处理分两步:
1.引入框架parent
<parent>
<groupId>com.test</groupId>
<artifactId>frame-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath/>
</parent>
2.引入frame-boostrap依赖
<dependency>
<groupId>com.test</groupId>
<artifactId>frame-boostrap</artifactId>
</dependency>
好了。接下来,请开始你的快乐之旅吧。