springboot自定义starter启动器

简介: springboot自定义starter启动器

说明

命名规约

官方命名:

  • spring-boot-starter-xxx

自定义命名:

  • xxx-spring-boot-starter

步骤

1、在idea中新建一个普通Maven模块:Jing-spring-boot-starter

2、新建一个Springboot模块:JIng-spring-boot-starter-aoutoconfigure

3、在starter中导入autoconfigure的依赖

<dependency>
    <groupId>com.lili</groupId>
    <artifactId>jing-springboot-starter-autoconfig</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

4、编写一个类用来测试

@ConfigurationProperties(prefix = "com.lili")
public class HelloProperties {
    private String name;
    private int age;
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public int getAge(){
        return this.age;
    }
    @Override
    public String toString() {
        return "HelloProperties{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class HelloService {
    private HelloProperties helloProperties;
    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }
    public HelloProperties getHelloProperties() {
        return helloProperties;
    }
    public void show(){
        System.out.println("你好,丽丽");
    }
}

5、编写一个配置类并注入bean进行测试

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
    @Autowired
    HelloProperties helloProperties;
    @Bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

6、在resources编写一个自己的META-INF\spring.factories,内容如下

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.lili.config.HelloServiceAutoConfiguration

7、新建一个Maven模块进行测试,需要导入自定义的启动器

<!--导入自定义的测试器进行测试-->
        <dependency>
            <groupId>com.lili</groupId>
            <artifactId>jing-springboot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

8、编写Controller

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;
    @RequestMapping("/helloStart")
    public HelloService getMessage(){
        return helloService;
    }
}

9、在properties里面可以给属性赋值

com.lili.name=张三
com.lili.age=23

10、运行然后访问

到这里,我们自己定义的starter就成功啦!

目录
相关文章
|
1月前
|
XML Java 数据格式
Springboot中自定义组件
Springboot中自定义组件
|
1月前
|
安全 Java Spring
SpringBoot2 | SpringBoot监听器源码分析 | 自定义ApplicationListener(六)
SpringBoot2 | SpringBoot监听器源码分析 | 自定义ApplicationListener(六)
61 0
|
1月前
|
设计模式 Java 机器人
SpringBoot3自动配置流程 SPI机制 核心注解 自定义starter
SpringBoot3自动配置流程 SPI机制 核心注解 自定义starter
|
1月前
|
缓存 Java Sentinel
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
|
1月前
|
Java 数据库 数据安全/隐私保护
【SpringBoot】Validator组件+自定义约束注解实现手机号码校验和密码格式限制
【SpringBoot】Validator组件+自定义约束注解实现手机号码校验和密码格式限制
259 1
|
3天前
|
Java
springboot自定义拦截器,校验token
springboot自定义拦截器,校验token
20 6
|
1天前
|
消息中间件 Java Maven
深入理解Spring Boot Starter:概念、特点、场景、原理及自定义starter
深入理解Spring Boot Starter:概念、特点、场景、原理及自定义starter
10 1
|
3天前
|
Java
springboot自定义log注解支持EL表达式
springboot自定义log注解支持EL表达式
13 0
|
7天前
|
前端开发 JavaScript Java
【Spring Boot】 深入理解Spring Boot拦截器:自定义设计与实现全攻略
【Spring Boot】 深入理解Spring Boot拦截器:自定义设计与实现全攻略
10 0
|
1月前
|
Java
Springboot 使用自定义注解结合AOP方式校验接口参数
Springboot 使用自定义注解结合AOP方式校验接口参数
Springboot 使用自定义注解结合AOP方式校验接口参数