SpringBoot:自定义starter

1、命名规范

官方命名:

  • 前缀:spring-boot-starter-xxx
  • 比如:spring-boot-starter-web

自定义命名:

  • xxx-spring-boot-starter
  • 比如:mybatis-spring-boot-starter

2、编写starter

  • 创建一个springboot项目

  •  导入相关依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        
        //导入了这个依赖才能使一些相关注解生效    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
  • 创建HelloProperties .java类
package com.wust.properties;

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

@ConfigurationProperties(prefix = "wust.hello")
public class HelloProperties {
    private String name;
    private int age;
    private String hobby;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    @Override
    public String toString() {
        return "HelloProperties{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", hobby='" + hobby + '\'' +
                '}';
    }
}

@ConfigurationProperties(prefix = "wust.hello")这个注解的作用就是将yml配置文件中以wust.spring开头的属性注入到HelloProperties.java类中对应的属性。

  • 创建HelloService.java类
package com.wust.service;

import com.wust.properties.HelloProperties;

public class HelloService {
    HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String sayHello(){
        return helloProperties.getName()+"对你说hello";
    }
}
  • 创建HelloServiceAutoConfiguration.java配置类
package com.wust.config;


import com.wust.properties.HelloProperties;
import com.wust.service.HelloService;
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(HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}
  1. @Configuration:声明为配置类
  2. @ConditionalOnWebApplication:当应用为web应用时配置才生效
  3. @EnableConfigurationProperties(HelloProperties.class):使@ConfigurationProperties注解生效,并将对应的标注有@ConfigurationProperties这个注解的类注册到IOC容器中
  • 在resources目录下创建 META-INF/spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wust.config.HelloServiceAutoConfiguration

  • 点击maven下的install, 将项目安装在本地maven中

3、新建SpringBoot项目进行测试

  • 导入相关依赖
        //导入上述自定义编写的starter
        <dependency>
            <groupId>com.wust</groupId>
            <artifactId>hello-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        //导入这个依赖,使在编写配置文件时会有提示
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>
        //导入web依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
  • 在properties配置文件中配置HelloPerties.java中的相关属性
wust.hello.name=张三
wust.hello.age=24
wust.hello.hobby=打篮球
  • 编写controller进行测试
package com.wust.springbootstartertest.controller;

import com.wust.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String hello(){
        return helloService.sayHello();
    }

}
  • 访问localhost:8080/hello

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值