首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Spring Boot系列-如何自定义Starter!

创建一个maven项目,在pom文件中添加如下依赖:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.wisdom

spring-boot-starter-hello

1.0-REALEASE

jar

org.springframework.boot

spring-boot-autoconfigure

2.0.0.RELEASE

org.springframework.boot

spring-boot-maven-plugin

spring-boot-autoconfigure此jar包中包含大量核心注解,包含条件注解等。创建properties属性类,用于读取属性。

@ConfigurationProperties(prefix="com.wisdom")

publicclassHelloServiceProperties{

privateStringname="wisdom";

privateStringhobby="basketball";

publicStringgetName() {

returnname;

}

publicvoidsetName(Stringname) {

this.name=name;

}

publicStringgetHobby() {

returnhobby;

}

publicvoidsetHobby(Stringhobby) {

this.hobby=hobby;

}

}

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

name=“wisdom”,hobby=“basketball”.

创建配置类

publicclassHelloServiceConfiguration{

privateStringname;

privateStringhobby;

publicStringgetName() {

return"name is "+name;

}

publicStringgetHobby() {

return"hobby is "+hobby;

}

publicvoidsetName(Stringname) {

this.name=name;

}

publicvoidsetHobby(Stringhobby) {

this.hobby=hobby;

}

}

创建自动配置类:

@Configuration

@EnableConfigurationProperties(HelloServiceProperties.class)

@ConditionalOnClass(HelloServiceConfiguration.class)

@ConditionalOnProperty(prefix="com.wisdom",value="enabled",matchIfMissing=true)

publicclassHelloServiceAutoConfiguration{

@Autowired

privateHelloServicePropertieshelloServiceProperties;

@Bean

@ConditionalOnMissingBean(HelloServiceConfiguration.class)

publicHelloServiceConfigurationhelloServiceConfiguration() {

HelloServiceConfigurationhelloService=newHelloServiceConfiguration();

helloService.setName(helloServiceProperties.getName());

helloService.setHobby(helloServiceProperties.getHobby());

returnhelloService;

}

}

@Configuration:表明此类是一个配置类,将变为一个bean被spring进行管理。@EnableConfigurationProperties:启用属性配置,将读取HelloServiceProperties里面的属性。@ConditionalOnClass:当类路径下面有HelloServiceConfiguration此类时,自动配置。@ConditionalOnProperty:判断指定的属性是否具备指定的值。@ConditionalOnMissingBean:当容器中没有指定bean是,创建此bean。在resources文件夹下面新建一个META-INF文件,并在下面创建spring.factories文件,将4中的配置类进行注册。至此,自定义的spring-boot-starter-hello编写完毕,当然springboot官方建议对于非官方的starter命名方式为xxx-spring-boot-starter。执行

将项目打成一个jar包。

新建一个springboot项目,在pom文件中添加刚刚打包的jar的坐标。

com.wisdom

spring-boot-starter-hello

1.0-REALEASE

在启动类上编写访问接口

@SpringBootApplication

@RestController

publicclassSpringboot03Application{

@Autowired

privateHelloServiceConfigurationhelloService;

publicstaticvoidmain(String[]args) {

SpringApplication.run(Springboot03Application.class,args);

}

@RequestMapping("/name")

publicStringgetName() {

returnhelloService.getName();

}

@RequestMapping("/hobby")

publicStringgetHobby() {

returnhelloService.getHobby();

}

}

启动此springboot项目,依次访问 localhost:8080/name

localhost:8080/hobby

由于没有在application.properties中添加指定属性,所以会使用默认的属性。接下来在application.properties添加我们自己的属性重启springboot项目,继续访问

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20200109A0IIQ500?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券
http://www.vxiaotou.com