美文网首页
0228 我的潘多拉

0228 我的潘多拉

作者: 李福春carter | 来源:发表于2020-02-29 01:18 被阅读0次
image.png

我的潘多拉

从一个故事说起。<br />从前,有个Java程序员非常喜欢写程序,喜欢研究源码,读英文文档。但是它在一家小公司里工作,公司的技术栈很陈旧。<br />
<br />单个系统代码中含有很多的xml配置,配置各种中间件的入口适配器,而不同的业务系统中都是类似的配置。<br />启动单个系统很慢。<br />启动依赖web组件,无法快速部署。<br />公共组件的依赖复杂,容易依赖冲突,版本陈旧。<br />在springcloud流行的时代,无法快速集成新的优秀组件。<br />
<br />而且,团队统一使用类似的项目模板,maven多模块,更换的阻力大。<br />使用统一项目模板可以更好的规范团队的代码结构,遵守公共的公共组件规约,减少了不同的工程师维护代码的成本。

现实很残酷,我爱springboot。<br />
<br />springboot不是新的东西,官网写的目标很强大:build everything ;<br />
<br />可以很好的对症下药,用了之后应该会神清气爽。<br />
<br />springboot提倡零xml,使用java config的模式取代配置代码,通过代码的方式,就有可能消除重复的配置。<br />springboot的启动速度很快,看了部分源码,有部分异步启动装配组件。<br />springboot为快速部署而生,内置了web容器无需配置tomcat.<br />springboot可以使用starter的方式使用公共组件,而且很好的解决了依赖冲突。

springboot积累了大量的最佳实践,为springcloud和各种大的云厂商提供很好的很好的支持。<br />而springcloud提供了成熟配套的微服务组件,技术更先进,在行业的通用性更强。<br />
<br />那么如何在热爱新技术跟现实的成就的工作中找到一条两全其美的高速公路呢?

如何打开上图中的潘多拉魔盒,连接现实与梦想。

简单直接:<br />直接把项目骨架改成基于springboot结构的就可以了;<br />团队习惯多模块,那就改造成多模块的,发布模块改为springboot就好了;<br />团队封装了很多公共组件,接着用,加个适配器改造成springbootstarter核心代码不变,使用xml接着配置也行,咱兼容并包。<br />
<br />剩下的事情就交给springboot,各种最佳实践,各种好用的组件,拿来就用,955不是梦,多点时间健身,多点时间陪家人。<br />
<br />扩大自己的技术影响力,让更多的小伙伴使用,出任CTO, 改善生活也不是梦。<br />

改造springboot

阿里巴巴的java编程规范大行其道,三层模型流行已久。<br />

<br /> image.png <br />

<br />
<br />分层对应maven的模块,简单画了一下层跟模块的对应关系图。<br />

<br /> image.png <br />
<br />
<br />原来的骨架结构如下图。<br /> image.png <br />

rest模块的包约定如下:<br />按照springboot的最佳实践,所有需要spring自动扫描的类放在xxxApplication类的同级或者下级目录中;

image.png

dal的包约定:

image.png

配置好mybatis的generator,可以方便生成持久层代码;

代码我也贴出来!

Archetype封装

achetype的流程:

image.png

开始流程前,先查阅一下maven的archetype的使用文档。上图是我阅读官网的文档之后画的流程图。下面来一步一步实现。<br />
<br />必须资源见红框:<br />
<br />

image.png <br />

1 新建项目

没什么好说的,直接使用maven提供的quickstart走起,删掉java代码。
清理好pom.xml,只保留mgav

2 配置pom的插件

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.springbootpractice.demo</groupId>
  <artifactId>archetypes-springboot</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
    <extensions>
      <extension>
        <groupId>org.apache.maven.archetype</groupId>
        <artifactId>archetype-packaging</artifactId>
        <version>3.1.1</version>
      </extension>
    </extensions>
 
  </build>

</project>

要点强调一下:<br />1 配置扩展archetype-packaging;

3 配置archetype-metadata

<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0
        http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd"
        xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        name="springboot-multi-module">

    <requiredProperties>
        <requiredProperty key="groupId">
            <defaultValue>com.leshiguang.industry.study</defaultValue>
        </requiredProperty>
        <requiredProperty key="artifactId">
            <defaultValue>demo-services</defaultValue>
        </requiredProperty>
        <requiredProperty key="package">
            <defaultValue>com.leshiguang.industry.study.demo</defaultValue>
        </requiredProperty>
        <requiredProperty key="version">
            <defaultValue>1.0-SNAPSHOT</defaultValue>
        </requiredProperty>
        <requiredProperty key="projectAuthor">
            <defaultValue>fuchun.li</defaultValue>
        </requiredProperty>
    </requiredProperties>

    <fileSets>
        <fileSet filtered="true" encoding="UTF-8">
            <directory/>
            <includes>
                <include>pom.xml</include>
                <include>.gitignore</include>
                <include>README.MD</include>
            </includes>
        </fileSet>
    </fileSets>

    <modules>
        <module id="${rootArtifactId}-api" dir="__rootArtifactId__-api" name="${rootArtifactId}-api">
            <fileSets>
                <fileSet filtered="true" encoding="UTF-8" packaged="true">
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </fileSet>
                <fileSet filtered="true" encoding="UTF-8">
                    <directory/>
                    <includes>
                        <include>pom.xml</include>
                        <include>.gitignore</include>
                        <include>README.MD</include>
                    </includes>
                </fileSet>
            </fileSets>
        </module>
        <module id="${rootArtifactId}-dal" dir="__rootArtifactId__-dal" name="${rootArtifactId}-dal">
            <fileSets>
                <fileSet filtered="true" encoding="UTF-8" packaged="true">
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </fileSet>
                <fileSet filtered="true" encoding="UTF-8" packaged="false">
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>jdbc.properties</include>
                        <include>generateConfig.xml</include>
                        <include>mapper/*.*</include>
                    </includes>
                    <excludes>
                        <exclude>comment.ftl</exclude>
                    </excludes>

                </fileSet>
                <fileSet filtered="true" encoding="UTF-8">
                    <directory></directory>
                    <includes>
                        <include>pom.xml</include>
                        <include>.gitignore</include>
                        <include>README.MD</include>
                    </includes>
                </fileSet>
            </fileSets>
        </module>
        <module id="${rootArtifactId}-biz" dir="__rootArtifactId__-biz" name="${rootArtifactId}-biz">
            <fileSets>
                <fileSet filtered="true" encoding="UTF-8" packaged="true">
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </fileSet>
                <fileSet filtered="true" encoding="UTF-8" packaged="false">
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </fileSet>
                <fileSet filtered="true" encoding="UTF-8">
                    <directory></directory>
                    <includes>
                        <include>pom.xml</include>
                        <include>.gitignore</include>
                        <include>README.MD</include>
                    </includes>
                </fileSet>
            </fileSets>
        </module>
        <module id="${rootArtifactId}-rest" dir="__rootArtifactId__-rest" name="${rootArtifactId}-rest">
            <fileSets>
                <fileSet filtered="true" encoding="UTF-8" packaged="true">
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </fileSet>
                <fileSet filtered="true" encoding="UTF-8" packaged="false">
                    <directory>src/test/java</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </fileSet>
                <fileSet filtered="true" encoding="UTF-8" packaged="false">
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </fileSet>
                <fileSet filtered="true" encoding="UTF-8">
                    <directory></directory>
                    <includes>
                        <include>pom.xml</include>
                        <include>.gitignore</include>
                        <include>README.MD</include>
                    </includes>
                </fileSet>
            </fileSets>
        </module>
        <module id="${rootArtifactId}-soa" dir="__rootArtifactId__-soa" name="${rootArtifactId}-soa">
            <fileSets>
                <fileSet filtered="true" encoding="UTF-8" packaged="true">
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </fileSet>
                <fileSet filtered="true" encoding="UTF-8" packaged="false">
                    <directory>src/test/java</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </fileSet>
                <fileSet filtered="true" encoding="UTF-8" packaged="false">
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.*</include>
                    </includes>
                </fileSet>
                <fileSet filtered="true" encoding="UTF-8">
                    <directory></directory>
                    <includes>
                        <include>pom.xml</include>
                        <include>.gitignore</include>
                        <include>README.MD</include>
                    </includes>
                </fileSet>
            </fileSets>
        </module>
    </modules>
</archetype-descriptor>

xml的规约如下:

基本结构如下图,英文还比较好理解。

image.png

节点里面的元素,看链接,不难。

4 配置模板资源

这里说明一下变量。

_ rootArtifactId 路径占位符_<br /> {rootArtifactId} 文件占位符<br />{package} 包名占位符

image.png

5 安装到本地

mvn clean install

<br />

6 本地使用

<br /> image.png

<br />

file

整理到团队,推广一下这个骨架,然后不断整理已有的中间件到这个骨架中迭代。<br />早日打开我的潘多拉。<br />
<br />
<br />骨架代码点我获取!

原创不易,转载请注明出处。

相关文章

  • 0228 我的潘多拉

    我的潘多拉 从一个故事说起。 从前,有个Java程序员非常喜欢写程序,喜欢研究源码,读英文文档。但是它在一家小公司...

  • 我的琴0228

    继续昨天的 你知道我在等你吗 外加新的和弦尝试 Dm E E7 可能有点熟悉 这几个和弦用了几次后就可以较不卡顿实现切换

  • 我的潘多拉

    银辉倾泄伴长鸥 那是海的呐喊 存在着无边无际想象 我把很多心事珍藏 蒙灰的画笔 老朋友的字迹 爱恋的经历 物什一样...

  • 潘多拉首饰的养护

    潘多拉首饰的养护 1. 如何养护我的潘多拉首饰? 建议在睡觉前或运动前取下潘多拉。避免接触汗水、香水、化学品、氯、...

  • 0228我的富有日记

    ?自我认可 1.晾衣服,抓紧洗毛衣厚衣服 2.花了个简易美美的妆。 ❤感恩他人 1.素姐的心理学沙龙教大家如何“说...

  • R

    0228 1.some refernce materialR CookbookR in Actionggplot2...

  • 0228

    【0228我在悦读】博妮 书名:人性的弱点 作者:卡耐基 篇目:1.1 金句: 001我们总是希望得到别人的赞扬,...

  • 0228

  • 0228

    1.二月的最后一天了。早上起来煮早餐,洗衣服,洗衣机的门没关好,所以一直洗不了,后来才弄好了。又睡了一个回笼觉,最...

  • 0228

    我会一直进步的 不是吗 差距不在这一年体现 而是在往后的很多很多年。

网友评论

      本文标题:0228 我的潘多拉

      本文链接:https://www.haomeiwen.com/subject/clkshhtx.html