基于自动配置jar包和SpringBoot以及注解一分钟快速搭建企业项目开发环境,主要包含DataBase、mybatis、Dubbo、log4j、RabbitMQ、redis及相关客户端配置等等

基于自动配置jar包和SpringBoot以及注解一分钟快速搭建企业项目开发环境,主要包含DataBase、mybatis、Dubbo、log4j、RabbitMQ、redis及相关客户端配置等等。

自动配置jar包特点

1、通过 条件注解 和 配置属性,即使项目中没有用到相应的中间件,依旧可以正常启动。
2、主要包含 datasource、MVC视图、mybatis、Httpclient及连接池、Dubbo、log4j、RabbitMQ、redis及相关客户端配置等等。
3、只需要设置相关的账号和密码,即可自动配置完成。
4、零配置,不需要编写任何的配置文件。
5、零侵入,不需要加入任何其他的代码,只需要一个类加一个注解完成。
6、超简单,需要配置的项都会有方法强制实现,不需要去记得相关的配置项。
7、比较全面,常用的项目开发配置和中间件都自动集成了。
8、内置自定义了一些开发的模版和配置,可进入对应的类查看默认参数。

一、新建项目

1、打开idea新建一个project,
在这里插入图片描述
2、下一步,设置相关的包名等信息,
在这里插入图片描述
3、下一步之后,再 下一步,中间什么都不用选,设置项目的名称,
在这里插入图片描述
在这里插入图片描述
4、点击 “Finish” 创建完成一个空项目。

二、添加依赖

1、在 resources 目录下建立 lib 目录,并把 springconfig.jar 包复制到 lib 目录,如下图所示。也可以用 maven 等其他方式,具体方案可以参照下面链接中的 “3.集成到项目” 这一节。

https://blog.csdn.net/u014374009/article/details/104514273

在这里插入图片描述
2、在项目的 pom.xml 文件中添加如下依赖:

除了以下的相关信息保留:

	<modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

其它的全部替换成以下的信息:


    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--项目快速配置依赖开始-->

        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--log4j2 日志框架-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.3.8.RELEASE</version>
        </dependency>-->

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>






        <!--<dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
        </dependency>-->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- 分页助手 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>3.7.5</version>
        </dependency>
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>0.9.1</version>
        </dependency>

        <!-- 通用Mapper -->
        <dependency>
            <groupId>com.github.abel533</groupId>
            <artifactId>mapper</artifactId>
            <version>2.3.4</version>
        </dependency>
        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- 连接池 -->
        <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp-spring</artifactId>
            <version>0.8.0.RELEASE</version>
        </dependency>

        <!-- httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.5</version>
        </dependency>

        <!-- JSP相关 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- Apache工具组件 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
        <!--<dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
            <version>1.4.0.RELEASE</version>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>


        <!-- SpringBoot 整合 Dubbo 选择这种依赖 -->
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!-- SpringMVC 整合 Dubbo 选择这种依赖 -->
        <!--<dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>dubbo</artifactId>
          <version>2.5.3</version>
          <exclusions>
            <exclusion>
              &lt;!&ndash; 排除传递spring依赖 &ndash;&gt;
              <artifactId>spring</artifactId>
              <groupId>org.springframework</groupId>
            </exclusion>
          </exclusions>
        </dependency>-->

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.3.3</version>
        </dependency>

        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <!--redis开始-->
        <!-- springconfig redis data and client jar-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.6.2</version>
        </dependency>


        <!--redis结束-->
        <!--redis-->
        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>-->

        <dependency>
            <groupId>springconfig</groupId>
            <artifactId>springconfig</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/springconfig.jar</systemPath>
        </dependency>



        <!--项目快速配置依赖结束-->


    </dependencies>

    <build>
        <plugins>
            <!-- 资源文件拷贝插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- java编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

三、引入自动配置

在项目的路径下 新建 config 包,并新建 MyProjectConfiguration 类继承 ProjectConfiguration 类,并且在 MyProjectConfiguration 类上添加 @AutoLoadConfiguration 注解,如下图所示。会自动实现相关的方法,比如设置数据库连接信息。到此项目已经自动配置完成了,可以开始开发了。

在这里插入图片描述

四、开始业务代码

1、数据库新建一张 book 表,字段如下,并填入相关的数据。
在这里插入图片描述
2、依次新建 entity、mapper、controller 等相关信息,如下图所示。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

五、启动测试

1、直接点击右上角的启动,或者进入 DemoApplication 类点击运行 main 方法,如下所示。
在这里插入图片描述
启动成功,
在这里插入图片描述
2、在浏览器输入 http://localhost:8080/book 访问并获得返回的 json 数据。
在这里插入图片描述
3、在我们设置的日志目录,会自动 按照不同的级别 生成日志记录文件。
在这里插入图片描述

六、相关源码

完整源码地址:https://github.com/YouAreOnlyOne/SpringAutoConfigDemo

相关专栏地址:https://blog.csdn.net/u014374009/category_9535972.html

技术介绍地址:https://me.csdn.net/u014374009

源码主页地址:https://github.com/YouAreOnlyOne

发布了66 篇原创文章 · 获赞 108 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/u014374009/article/details/104668794