自己编写一个SpringBoot的Starter插件项目

用过SpringBoot的同学相信都不会陌生POM里面引用的很多第三方的Starter插件,只要引用进项目,按照配置一下properties属性就可以自然使用了,比如Mybatis等等,觉得很酷,现在自己动手也编写一个这样的插件,以提供给别的项目,方便使用,更重要的是把相同功能和性质的代码封装起来,即插即用。

1.pom的引用

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-autoconfigure</artifactId>
  <version>2.0.0.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <version>2.0.0.RELEASE</version>
  <optional>true</optional>
</dependency>
2.resources文件夹下面建立文件夹META-INF,里面建立一个文件,如:spring.factories,内容如下,路径为自己的配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=USplus.configuration.UsServiceAutoConfiguration

3.工程结构目录,这个因人而异

4.UsServiceAutoConfiguration是加载时候的配置类

package USplus.configuration;

import USplus.UsProperties.UsProperties;
import USplus.UsService.UsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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;

@Configuration
@EnableConfigurationProperties(UsProperties.class)
@ConditionalOnClass(UsProperties.class)
@ConditionalOnProperty(prefix = "spring.person", value = "enabled", matchIfMissing = true)
public class UsServiceAutoConfiguration {

    @Autowired
    private UsProperties usProperties;

    @Bean
    @ConditionalOnMissingBean(UsService.class)  // 当容器中没有指定Bean的情况下,自动配置UsService类
    public UsService usService(){
        UsService usService = new UsService(usProperties);
        return usService;
    }


}

5.自定义属性参数类,UsProperties

package USplus.UsProperties;

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

@ConfigurationProperties(prefix = "spring.person")
public class UsProperties {

    private String where;

    private String when;

    private String who;

    private String what;

    private String why;

    public String getWhere() {
        return where;
    }

    public void setWhere(String where) {
        this.where = where;
    }

    public String getWhen() {
        return when;
    }

    public void setWhen(String when) {
        this.when = when;
    }

    public String getWho() {
        return who;
    }

    public void setWho(String who) {
        this.who = who;
    }

    public String getWhat() {
        return what;
    }

    public void setWhat(String what) {
        this.what = what;
    }

    public String getWhy() {
        return why;
    }

    public void setWhy(String why) {
        this.why = why;
    }

    @Override
    public String toString() {
        return "UsProperties{" +
                "where='" + where + '\'' +
                ", when='" + when + '\'' +
                ", who='" + who + '\'' +
                ", what='" + what + '\'' +
                ", why='" + why + '\'' +
                '}';
    }
}

6.自定义想要做什么事的service,UsService

package USplus.UsService;

import USplus.UsProperties.UsProperties;

public class UsService {

    private UsProperties usProperties;


    public UsService(UsProperties usProperties){
        this.usProperties = usProperties;
    }


    public String wh(){
        return usProperties.toString();
    }
}

7.打包即可引用了,我的groupId如下

<groupId>USplus</groupId>
<artifactId>us</artifactId>
<version>1.0-SNAPSHOT</version>

启动后,springboot自动加载配置的属性,并且返回,我配置的是when,who,where,what,why,简单的一个小例子,初学乍练,欢迎批评指正!

 
 
 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 可以参考Spring官网上的教程,按照步骤来搭建Springboot项目。首先,在官网上下载最新的Spring Boot发行版本,然后从发行资源包中抽取所有文件,接着创建一个Maven项目,把抽取的文件导入到项目中,最后使用Maven构建Spring Boot项目。 ### 回答2: 搭建一个Spring Boot项目可以按照以下步骤进行: 1. 确保电脑已经安装了JDK和Maven,并且配置了相应的环境变量。 2. 在IDE中创建一个新的Maven项目,选择Spring Initializr作为模板。 3. 在Initializr网站(https://start.spring.io/)上选择需要的项目配置。可以根据项目需求选择不同的选项,如语言、Spring Boot版本、依赖等。 4. 生成配置文件和pom.xml文件,下载到本地。 5. 将下载好的项目导入到IDE中。 6. 根据项目需求,添加相应的Controller、Service、Repository等类。 7. 设置项目的配置信息,如数据库连接、端口号等,可以在application.properties或application.yml文件中进行配置。 8. 编写业务逻辑代码,实现项目的功能。 9. 运行项目,在IDE中选择相应的启动类,点击运行按钮。 10. 访问项目的接口或页面,检查项目是否正常运行。 以上只是一个简单的搭建Spring Boot项目的步骤,具体的操作还需要根据实际情况进行调整。在项目的开发过程中,还需要根据实际需求引入相应的依赖,配置数据库等,完成项目基本的功能开发。 最后,可以使用Maven打包项目成可执行的jar包,部署到服务器上进行运行,实现项目的上线发布。 ### 回答3: 搭建一个Spring Boot项目可以按照以下步骤进行: 1. 安装开发环境 - 首先需要安装Java JDK和Maven,确保已经正确配置好Java和Maven的环境变量。 2. 创建项目 - 在命令行中使用Maven的Archetype插件创建Spring Boot项目。执行以下命令: ``` mvn archetype:generate -DgroupId=com.example -DartifactId=demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ``` 该命令会在当前目录下创建一个名为"demo"的Spring Boot项目。 3. 添加Spring Boot依赖 - 在项目的pom.xml文件中添加Spring Boot的依赖。一般情况下,需要添加"spring-boot-starter-web"依赖,以支持Web应用的开发。 4. 编写代码 - 创建一个Java类作为项目的入口,使用@SpringBootApplication注解标记该类为Spring Boot应用的启动类。在这个类中,可以编写业务逻辑代码、配置文件等。 5. 运行项目 - 在项目的根目录下执行以下命令启动应用: ``` mvn spring-boot:run ``` 如果一切顺利,应用将启动并监听默认的端口8080。 以上是创建一个简单的Spring Boot项目的基本步骤。根据项目的需求,可能还需要做其他的配置与扩展,如添加数据库依赖、配置文件等。开发过程中可以参考Spring Boot官方文档和相关教程来实现特定的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值