自定义starter

	启动器模块是一个 空 jar 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库;
	前缀:spring-boot-starter-xxx
	比如:spring-boot-starter-web
	自定义命名:xxx-spring-boot-starter
	比如:mybatis-spring-boot-starter

编写启动器
新建空项目 spring-boot-starter-diy
新建Maven模块:lxs -spring-boot-starter
在这里插入图片描述

新建Springboot模块:lxs-spring-boot-starter-autoconfigure

![在这里插入图片描述](https://img-blog.csdnimg.cn/20210529161215478.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDU0MTI0Ng==,size_16,color_FFFFFF,t_70

在starter 中 导入  autoconfigure 依赖!
<dependencies>
	<dependency>
    	<groupId>com.kuang</groupId>
    	<artifactId>lxs-spring-boot-starter-autoconfigure</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
	</dependency>
</dependencies>
autoconfigure 项目多余的文件都删掉,Pom。xml中留下一个 starter,这是启动器基本配置

编写自己的服务
package com.kuang;

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();
}
}

编写HelloProperties 一个配置类
package com.lxs;

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

@ConfigurationProperties(prefix = "lxs.hello")
public class HelloProperties {

private String prefix;
private String suffix;

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

编写自动配置类并注入bean
package com.lxs;

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;

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(LxsProperties.class)
public class LxServiceAutoConfiguration {

@Autowired
LxsProperties lxsProperties;

@Bean
public LxsService lxsService(){
    LxsServiceservice = new LxsService();
    service.setHelloProperties(helloProperties);
    return service;
}
}

在resources编写自己的 META-INF\spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.lxs.LxsServiceAutoConfiguration

编写完成后,可以安装到maven仓库中,新建项目测试写的启动器
1、新建SpringBoot 项目
2、导入自己写的启动器

<dependency>
	<groupId>com.lxs </groupId>
	<artifactId>lxs-spring-boot-starter</artifactId>
	<version>1.0-SNAPSHOT</version>
</dependency>

3、编写HelloController 测试自己写的接口
package com.lxs.controller;

@RestController
public class LxsController {

@Autowired
LxsService lxsService;

@RequestMapping("/hello ! sb")
public String lxs(){
    return helloService.sayHello("sb");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值