自己编写创建简单的SpringBoot启动器Starter

一,什么是SpringBoot

       1.1 概念

        是 Spring 的子项目,主要简化 Spring 开发难度,去掉了繁重配置,提供各种启动器,可以让程序员很快上手,节省开发时间.

        1.2 SpringBoot启动过程

        springboot启动类里的main方法中有一个叫做run()的方法,在run方法中必须要有一个@SpringBootApplication的注解类。@SpringBootApplication包含了@EnableAutoConfiguration的注解,该@EnableAutoConfiguration注解提供自动配置的功能,在@EnableAutoConfiguration中还包含@Import({AutoConfigurationImportSelector.class})注解。AutoConfigurationImportSelector这个注解提供了自动配置的选择器类,能够自动选择装载配置类。

二,启动器有什么作用?

1.引用启动类可以将所有的文件和jar包拷贝下来

2.能够自动帮助加载配置(文件里的配置项)

3.springboot项目启动的时候按照启动器里的配置做增强处理等

三,创建自己的启动器Starter

        我们上面提到了启动类的一些作用,可以从中看到启动类的很多优点,既然它有那么多的好处,那么下面就让我们一起创建第一个属于自己的启动类吧。

1.首先创建一个maven项目,引入SpringBoot的自动配置jar包

        1.1创建maven项目

 其中ArtifactId:qy156-spring-boot-starter 为依赖的标准格式

        2.2引入的jar包如下

<dependencies>
    <!-- 引入springboot的自动配置jar包-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
        <version>2.7.4</version>
    </dependency>
</dependencies>

2.创建一个实体类(以Student为例),以及对应的配置文件,并自动读取properties中的配置(进行配置,使其可以自动实例化该对象)

        2.1创建实体类

package entity;

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

/**
 * @author :Wqw
 * @date :Created in 2022/10/10 17:32
 * @description:学生类
 * @modified By:
 * @version:
 */

@Component//配置bean
@ConfigurationProperties(prefix = "student")//根据前缀自动加载配置文件
public class Student {
    /**
     * 创建一些属性
     */
    private int age;
    private String name;
    private String message;

    /**
     * 无参构造器
     */
    public Student() {
    }

    /**
     * 有参构造器
     */
    public Student(int age, String name, String message) {
        this.age = age;
        this.name = name;
        this.message = message;
    }

    /**
     * get和set方法
     */
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

        2.2创建对应配置类,自动读取并加载

student.id=12138
student.name=xiaoming
student.message=good good study,day day up

3.创建一个service层的bean,随便写一个方法

package service;

import entity.Student;

/**
 * @author :Wqw
 * @date :Created in 2022/10/10 19:17
 * @description:service层
 * @modified By:
 * @version:
 */
public class StudentService {
    Student student;

    /**
     * get和set方法
     */
    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    /**
     * 写个方法
     */
    public void getMessage() {
        System.out.println(student.getMessage());
    }
}

4.创建自动配置类

        //如果满足条件,包含类StudentService
        @ConditionalOnClass({StudentService.class})
        //自动配置属性,实体类属性
        @EnableConfigurationProperties(Student.class)

import entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import service.StudentService;

/**
 * @author :Wqw
 * @date :Created in 2022/10/10 19:27
 * @description:第一个自动配置类
 * @modified By:
 * @version:
 */
@org.springframework.context.annotation.Configuration
//如果满足条件,包含类StudentService
@ConditionalOnClass({StudentService.class})
//自动配置属性,实体类属性
@EnableConfigurationProperties(Student.class)
public class MyStarterAutoConfiguration {
    @Autowired
    Student student;

    /**
     *  
     *
     * @param  
     * @return service.StudentService
     * @create by: Wqw
     * @description:  实例化StudentService的Bean
     * @create time: 2022/10/10 19:32
     */
    @Bean
    public StudentService studentService() {

        StudentService studentService = new StudentService();
        studentService.setStudent(student);
        return studentService;
    }
}

5.创建spring.factories配置文件(将上面的配置类加到该配置文件中)

 

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
config.MyStarterAutoConfiguration

        # Auto Configure
        org.springframework.boot.autoconfigure.EnableAutoConfiguration=\(固定配置)

        下面的配置config.MyStarterAutoConfiguration(是自己写的自动配置全路径)

6.打包发布到本地仓库

        在控制台输入mvn install,将其打成一个jar包

         按照保存的路径:D:\maven-respository\com\aaa\qy156-spring-boot-starter找到qy156-spring-boot-starter jar包

7.在另外一个普通的springboot项目(新建的springboot项目)中使用之前创建好的starter

        7.1 创建普通的springboot项目

         7.2 引用之前打包好的jar包

其路径在

打开文本后

8.测试:在另外一个普通的springboot项目中测试

        8.1 配置新项目的application.properties

student.id=12138
student.name=xiaoming
student.message=good good study,day day up

        8.2 利用自带的测试类进行测试

        测试代码如下:

package com.aaa;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import service.StudentService;

@SpringBootTest
class DemoStarterApplicationTests {

    @Autowired
    StudentService studentService;

    @Test
    void contextLoads() {
        studentService.getMessage();
    }

}

9,测试结果

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值