springboot使用自定义starter,springboot学习一

1.说明

springboot是一个高度集成spring和springmvc的框架,此外在使用其他框架如msyql、mybtis等时,
只需要添加他们的starter既可使用。
在此,笔者创建一个maven项目创建两个模块,一个模块打包成jar,然后在test模块中调用它的功能来发送请求。

2.创建工程

1.创建一个空项目,名为hello-spring-boot-starter在这里插入图片描述
2.添加子模块,创建一个springboot工程,名为hello-spirng-boot-starter-autoconfigure
在这里插入图片描述
在这里插入图片描述
3.hello-spirng-boot-starter-autoconfigure的目录结构,删除启动类、test目录,在resources目录下创建META-INF/spring.factories文件,
在这里插入图片描述
同时删除pom文件里的test插件
在这里插入图片描述

3.编码

1.我们在hello-spirng-boot-starter-autoconfigure实现一个sayHello功能,然后在测试模块中调用。
创建HelloProperties 类,作为配置类,目的是后面使用这个自动配置,可以测试模块的application.properties 文件中配置:

package com.wyu.hellospirngbootstarterautoconfigure.properties;

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

@ConfigurationProperties(prefix = "com.wyu")
public class HelloProperties {
    /**
     * 默认name为xiaoming
     */
    private String name = "xiaoming";

    public String getName() {
        return name;
    }

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

@ConfigurationProperties配置此注解可以自动导入application.properties配置文件中的属性,前提需要指定属性前缀prefix。如果application.properties文件中未指定相应属性,则使用默认的,默认name为xiaoming

2.创建HelloService实现类

package com.wyu.hellospirngbootstarterautoconfigure.service;

import com.wyu.hellospirngbootstarterautoconfigure.properties.HelloProperties;
import org.springframework.beans.factory.annotation.Autowired;

public class HelloService {

    @Autowired
    private HelloProperties helloProperties;

    public String sayHello(String name) {
        return  helloProperties.getName() + " say hello to " + name;
    }

}

3.创建HelloAutoConfig自动配置类

package com.wyu.hellospirngbootstarterautoconfigure.config;

import com.wyu.hellospirngbootstarterautoconfigure.properties.HelloProperties;
import com.wyu.hellospirngbootstarterautoconfigure.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties({HelloProperties.class})
@Import(HelloService.class)
@ConditionalOnProperty(prefix = "com.wyu", value = "enabled", matchIfMissing = true)
public class HelloAutoConfig {

}

  • @EnableConfigurationProperties:启用属性配置,将读取HelloProperties里面的属性。
  • @ConditionalOnProperty:判断指定的属性是否具备指定的值。

4.打包hello-spirng-boot-starter-autoconfigure模块

通过mvn clean install将hello-spirng-boot-starter-autoconfigure打包成一个jar包

mvn clean install
在这里插入图片描述

5.测试

1.创建一个测试工程,添加我们自定义的starter的依赖

<dependency>
   <groupId>com.wyu</groupId>
   <artifactId>hello-spirng-boot-starter-autoconfigure</artifactId>
   <version>0.0.1-SNAPSHOT</version>
</dependency>

2.在测试工程的application.properties配置文件中,我们可以添加配置

com.wyu.name=dacheng

3.编写controller

package com.wyu.test;

import com.wyu.hellospirngbootstarterautoconfigure.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class controller {
    @Autowired
    private HelloService helloService;

    @RequestMapping("/sayHello")
    public String sayHello(String name){
        return helloService.sayHello(name);
    }
}

4.运行测试工程的效果,我们可以看到在test通过添加hello-spirng-boot-starter-autoconfigure的starter成功调用其功能
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高并发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值