SpringBoot自定义starter

1、SpringBoot自定义starter

1.1、创建一个空工程
1.2、添加两个模块
  1. 空Maven项目lu-hello-spring-boot-starter
  2. SpringBoot项目lu-hello-spring-boot-stater-autoconfigure
1.3、lu-hello-spring-boot-starter

在Pom中引入autoconfigure,autoconfigure项目见1.4

<dependencies>
    <dependency>
        <groupId>com.lu</groupId>
        <artifactId>lu-hello-spring-boot-stater-autoconfigure</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>
1.4、lu-hello-spring-boot-stater-autoconfigure
  1. pom

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
    
  2. HelloProperties,与配置文件绑定

    @ConfigurationProperties("lu.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;
        }
    }
    
  3. HelloService,编写相关业务

    /**
     * 默认不要放在容器中
     */
    public class HelloService {
    
        @Autowired
        HelloProperties helloProperties;
    
        public String sayHello(String userName){
            return helloProperties.getPrefix() + ":"+userName+">"
            +helloProperties.getSuffix();
        }
    
    }
    
  4. HelloServiceAutoConfiguration,自动配置类

    @Configuration
    @EnableConfigurationProperties(HelloProperties.class)//将HelloProperties添加进容器中
    public class HelloServiceAutoConfiguration {
    
        @ConditionalOnMissingBean(HelloService.class)
        @Bean
        public HelloService helloService(){
            HelloService helloService = new HelloService();
            return helloService;
        }
    
    }
    
  5. 编写spring.factories

    在类路径下resources下创建文件META-INF/spring.factories

    SpringBoot启动时会加载spring.factories里的类进入容器

    # Auto Configure
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.lu.hello.auto.HelloServiceAutoConfiguration
    
  6. 对两个模块进行打包,进Maven仓库

    lifecycle -> clean -> install

1.5、测试
  1. 创建一个SpringBoot项目

  2. 引入刚刚打包在Maven仓库的依赖

            <dependency>
                <groupId>org.lu</groupId>
                <artifactId>lu-hello-spring-boot-starter</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
    
  3. 编写配置文件application.properties

    lu.hello.prefix=你好
    lu.hello.suffix=嘻嘻
    
  4. 编写Controller

    @RestController
    public class HelloController {
    
        @Autowired
        HelloService helloService;
    
        @GetMapping("/sayHello")
        public String sayHello(){
            String name = helloService.sayHello("小明");
            return name;
        }
    
    }
    
  5. 完成测试

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值