SpringBoot-自定义starter包

一、Springboot自动装配原理

我们在springboot项目的pom.xml文件中,经常看到 spring-boot-start-xxx 或者 xxx-spring-boot-starter 的依赖包,那它们到底做了什么,我们自己可以动手做一个吗?

在springboot中,帮我们创建了许多的自启动类,在Springboot项目运行时,自动帮助我们从配置文件中读取数据,并加载到相应的XXXProperties中,内置的XXXService服务引入properties中的属性执行业务逻辑。重要的是,Springboot自启动类XXXAutoConfigration帮助我们从配置文件中引入数据,再类的内部将XXXService注入到IOC容器中,并且给XXXService放如对应功能XXXPreperties中的相关信息。

这样,当我们导入该包,并且启动SpringBoot之后,Springboot会自动将很多类似的功能模块自动注入到IOC容器中供我们使用,而我们只需要导入包,并且在application.properties配置文件内部添加设置好属性的参数就可以了,简化了我们的配置过程,简化了繁杂的XML配置方式.


二、自定义spring-boot-starter步骤

1、创建一个spring-boot-starter-div 的 springboot工程,在内部新建一个llj-spring-boot-starter的maven项目,以及一个llj-spring-boot-starter-autoconfigure的springboot项目。
在这里插入图片描述

2、在llj-spring-boot-starter-autoconfigure中只保留pom.xml文件及空的java文件,

pom.xml文件中只保留spring-boot-starter
3、在llj-springboot-starter的pom文件中只保留llj-spring-boot-starter-autoconfigure
llj-spring-boot-starter的pom文件中
4、在自动配置包中添加XXXProperties,表示从配置文件的llj.hello之后提取属性参数,并set注入到HelloProperties类中

package com.llj;

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

/**
 * @author leo
 * @create 2021-06 -20-16:01
 */

@ConfigurationProperties(prefix = "llj.hello")
public class HelloProperties {
    private String prefix;
    private String suffix;

    public void setPrefix(String prefix){
        this.prefix = prefix;
    }
    public String getPrefix(){
        return prefix;
    }
    public void setSuffix(String suffix){
        this.suffix = suffix;
    }
    public String getSuffix(){
        return suffix;
    }
}

5、将XXXproperties类中的属性注入到XXXService类中,通过参数实现对应的业务逻辑

	package com.llj;
/** 
 * @author leo
 * @create 2021-06 -20-16:01
 */
public class HelloService {
    HelloProperties helloProperties;

    public HelloProperties getHelloProperties(){
        return helloProperties;
    }
    public void setHelloProperties(HelloProperties helloProperties){
        this.helloProperties = helloProperties;
    }
    public String sayHello(String name){
        return helloProperties.getPrefix() + name + helloProperties.getSuffix();
    }
}

6、实现自动装配方法

package com.llj;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author leo
 * @create 2021-06 -20-16:09
 */
@Configuration//表明这是一个配置类,就类似xml配置文件,可以实现bean注入
@ConditionalOnWebApplication // 只有当前项目是Web项目的条件下,才会生效
@EnableConfigurationProperties(HelloProperties.class) //让HelloProperties类中的自动配置生效
public class HelloServiceAutoConfiguration {
    @Autowired
    HelloProperties helloProperties;
    @Bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

7、我们还需要在我们的properties包下添加META_INF/spring.factories文件,

org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.llj.HelloServiceAutoConfiguration

并对llj-spring-boot-starter-autoconfigure,与llj-spring-boot-starter进行maven中install的打包。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210620173949890.png)

8、这样我们的自动扫描包就完成了。我们可以新创建一个springboot工程,导入我们自己写的llj-spring-boot-starter为了方便、这里直接添加HelloController类

package com.llj.controller;



import com.llj.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author leo
 * @create 2021-06 -20-16:28
 */
@RestController
//  @RestController
// 是@ResponseBody和@Controller的组合注解
public class HelloController {
    @Autowired
    HelloService helloService;

    @RequestMapping("/hello")//@RequestMapping 配置url映射
    public String hello(){
        return helloService.sayHello("zxc");
    }
}

别忘了在application.properties中添加配置

llj.hello.prefix="ppp"
llj.hello.suffix="sss"

好啦,我们快点启动该springboot项目,输入localhost:8080/hello 试试
成功啦!!!
这就是基本的springboot自动装配原理,如果第一次接触这个,别光看,你也快去试一试吧。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值