自定义spring-boot-starter组件
新建一个starter组件:依赖spring-data-redis,spring-boot-starter-data-redis
作用: 输入一个图片url,返回一个IMEI集合,通过百度ai ocr标准文本识别
组件module:
<dependency> <groupId>com.aihuishou.mta</groupId> <artifactId>imei-spring-boot-starter</artifactId> <version>1.0.5</version> </dependency>
-
新建Properties
import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; @Data @ConfigurationProperties(prefix = "imei") public class ImeiProperties { @Value("${baidu.imei-url}") private String baiduUrl; @Value("${baidu.imei-ak}") private String ak; @Value("${baidu.imei-sk}") private String sk; @Value("${redis-timeout}") private Long timeout; }
-
模版调用入口
import com.aihuishou.mta.brick.common.starter.imei.config.ImeiProperties; import com.aihuishou.mta.brick.common.starter.imei.util.ImeiUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import java.util.List; public class ImeiService { @Autowired private RedisTemplate redisTemplate; private String baiduUrl; private String ak; private String sk; private Long timeout; public ImeiService(ImeiProperties properties){ this.baiduUrl = properties.getBaiduUrl(); this.ak = properties.getAk(); this.sk = properties.getSk(); this.timeout = properties.getTimeout(); } public List<String> getImeis(String imageUrl){ return ImeiUtil.generalBasic(redisTemplate,imageUrl,baiduUrl,ak,sk,timeout); } }
-
新建AutoConfiguration自动装配
import com.aihuishou.mta.brick.common.starter.imei.ImeiService; import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableConfigurationProperties(ImeiProperties.class) public class ImeiAutoConfiguration { @Bean public ImeiService getImeiService(ImeiProperties properties){ ImeiService imeiService = new ImeiService(properties); return imeiService; } }
mvn clean install
-
在需要自定义组件的项目pom引入依赖
<dependency>
<groupId>com.aihuishou.mta</groupId>
<artifactId>imei-spring-boot-starter</artifactId>
<version>1.0.5</version>
</dependency> -
在applicaition中或者配置中心配置ImeiProperties的相关属性
baidu.imei-url=https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic baidu.imei-ak=CHxcZ96ItWgCBBr51nOKWEfh baidu.imei-sk=3oGNRDkZQDGUwgl1wruVFHGHvIFy3VCf redis-timeout=300000
-
编写测试controller
import com.aihuishou.mta.brick.common.starter.imei.ImeiService; import lombok.AllArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/imei") public class ImeiController { @Autowired private ImeiService imeiService; @GetMapping public List<String> getImei(@RequestParam(value = "url") String imageUrl){ return imeiService.getImeis(imageUrl); } }
需要完整源码的请留言,会第一时间处理!