springboot-plugin极简实践(一)

x33g5p2x  于11个月前 转载在 Spring  
字(6.4k)|赞(0)|评价(0)|浏览(101)

前言

网上看到一句这样的描述:
Spring Boot可以说是至少近5年来Spring乃至整个Java社区最有影响力的项目之一,也被人看作是:Java EE开发的颠覆者
本人深表同意
使用springBoot是一个很容易的事
但要基于springBoot的简单理论去搭建自己的整体框架,就需要学习和理解springBoot-Plugin,以它的方式来构建,让自己也能够享受到springBoot的优雅


springBoot-plugin极简实践(一)

实践的价值,一定要简单,且突然核心目标,我这边使用三个工程就是基于这个理念编写,分别是 jdbc,query,queryExample (顺序体现了依赖过程)

1、 jdbc工程为核心工程,它体现spring-boot的优雅的配置和代码的结合

maven 依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>2.5.0</version>
    </dependency>
</dependencies>

自动装载properties的简单对象

package com.riso.jdbc.common.object;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "jdbc.custom")
public class CustomProperties {

    private String url;
    private String user;
    private String password;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "CustomProperties{" +
                "url='" + url + '\'' +
                ", user='" + user + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

极简的核心加载配置的代码,省略了实现

package com.riso.jdbc.common;

import com.riso.jdbc.common.object.CustomProperties;
import org.springframework.stereotype.Component;

@Component
public class ConnectionManager {

    private boolean LOAD = false;

    public boolean isLoad() {
        return LOAD;
    }

    /** * 建立工厂 */
    public void establishFactory(CustomProperties customProperties) {
        LOAD = true;
        System.out.println("establishFactory param:" + customProperties.toString());
        //...
    }

}

自动加载类

package com.riso.jdbc;

import com.riso.jdbc.common.ConnectionManager;
import com.riso.jdbc.common.object.CustomProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@ConditionalOnProperty(value = "jdbc.custom.url", matchIfMissing = false)
@Configuration
@EnableConfigurationProperties({CustomProperties.class})
public class JdbcAutoConfigure {

    @Autowired
    private CustomProperties customProperties;

    @Autowired
    private ConnectionManager connectionManager;

    @Bean(name = "customConnectionManager")
    public ConnectionManager customConnectionManager() {
        System.out.println("jdbc try init");

        if (connectionManager.isLoad()) {
            System.out.println("jdbc get manager");
            return connectionManager;
        }
        System.out.println("jdbc init");
        connectionManager.establishFactory(customProperties);

        return connectionManager;
    }

}

在resources\META-INF 新建spring.factories 文件,内容如下

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.riso.jdbc.JdbcAutoConfigure

2、 query 基于jdbc 最核心的配置的另一种也是体现工程拆分的理念,它实现了查询数据库的场景

maven 引入jdbc既可

<dependencies>
    <dependency>
        <groupId>com.riso</groupId>
        <artifactId>jdbc</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>
</dependencies>

实现jdbc的查询bean

package com.riso.query.dao;

import com.riso.jdbc.common.ConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class Query {

    @Autowired
    private ConnectionManager connectionManager;

    public List<?> all() {
        if (connectionManager.isLoad() == false) {
            throw new RuntimeException("not init");
        }
        System.out.println("all data");
        return Arrays.asList("testData");
    }
}

3、 query-example 例子工程,体现如何使用自定义的plugin

maven依赖,关联query既可

<dependencies>
    <dependency>
        <groupId>com.riso</groupId>
        <artifactId>query</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>
</dependencies>

配置 resources/application.properties 文件
如果jdbc.custom.url 配置项不存在,不会自载加载jdbc 的代码逻辑

jdbc.custom.url=jdbc.mysql.test
jdbc.custom.user=testUser
jdbc.custom.password=testPassword

一个独立的springboot 工程的初始化类 并附加一个运行查询逻辑功能,最简化的example

package com.riso;

import com.riso.query.dao.Query;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.List;

@SpringBootApplication
public class QueryExampleApplication {

    public static void main(String[] args) {
        System.out.println("queryExampleBefore");
        ConfigurableApplicationContext context = SpringApplication.run(QueryExampleApplication.class, args);

        //查询尝试
        Query query = context.getBean(Query.class);
        List<?> list = query.all();
        System.out.println("queryData listSize:" + list.size());
        System.out.println("queryExampleAfter");
    }

}

run QueryExampleApplication 后的日志:

queryExampleBefore

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.0)

[2021-06-27 22:54:06.985] - 14452 信息 [main] --- com.riso.QueryExampleApplication: Starting QueryExampleApplication using Java 1.8.0_131 on DESKTOP-MMHNK85 with PID 14452 (D:\code\riso\github\riso-jay\riso\riso-parent\springboot-plugin-example\query-example\target\classes started by ok in d:\IdeaProjects\githup-jack)
[2021-06-27 22:54:06.996] - 14452 信息 [main] --- com.riso.QueryExampleApplication: No active profile set, falling back to default profiles: default
jdbc try init
jdbc init
establishFactory param:CustomProperties{url='jdbc.mysql.test', user='testUser', password='testPassword'}
[2021-06-27 22:54:07.596] - 14452 信息 [main] --- com.riso.QueryExampleApplication: Started QueryExampleApplication in 1.032 seconds (JVM running for 1.421)
[2021-06-27 22:54:07.598] - 14452 信息 [main] --- org.springframework.boot.availability.ApplicationAvailabilityBean: Application availability state LivenessState changed to CORRECT
[2021-06-27 22:54:07.600] - 14452 信息 [main] --- org.springframework.boot.availability.ApplicationAvailabilityBean: Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
all data
queryData listSize:1
queryExampleAfter

Process finished with exit code 0

关于springboot-plugin的实践(一)总结

大部分工程,随着项目的迭代,不同技术人员变更,工程质量越来越没有保证

以前我借鉴spring 全家桶思维 在工程进行实践和反省,所以我推荐应该要切换到spring-boot plugin思路来进行工程实践 ,它是一个更优秀的业务框架

所以学习springboot,掌握springboot-plugin的知识并实践在项目中,是非常有必要的。

本章主要还是围绕着配置这块,后续章结还有plugin部署、测试等实践

作者:朱杰 又称 杰克码

相关文章

热门文章

更多