Spring Boot - 自定义Starter封装

Spring Boot - 自定义Starter封装

在Springboot封装一个自定义的Starter的一个Demo,从创建一个模块->封装starter->使用。 @pdai

思路

  • 1 - 创建一个Demo Project,模拟一个需要被封装的DemoModule模块,其中核心方法为exeModuleMethod
  • 2 - 通过starter封装可以直接初始化DemoModule的实例到Spring容器
  • 3 - 在Maven中引入starter,且在yml中配置相应到参数即可直接初始化DemoModule的实例
  • 4 - 在应用中注入DemoModule即可使用其exeModuleMethod方法

新建一个com.pdai.demo项目

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.pdai</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>
        <!-- 注意不要引入Springboot模块-->
    </dependencies>

    <build>
        
    </build>

</project>

创建一个测试模块

package com.pdai.demo.module;

/**
 *
 */
public class DemoModule {
   

    private String version;

    private String name;

    public String exeModuleMethod() {
   
        return "Demo module, name = "   name   ", version = "   version;
    }

    public String getVersion() {
   
        return version;
    }

    public void setVersion(String version) {
   
        this.version = version;
    }

    public String getName() 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值