← 文章归档 26-Spring Boot MySDK 2022-04-13 约 18 分钟阅读 阅读 — 次
Spring MySDK
将自己写好的部分封装,作为SDK给其他部分使用
Spring 版本均为2.3.1
1. mysdk 项目
MysdkApplication没有修改
目录结构
1.1. 创建项目
创建一个新项目,使用官方默认提供的初始化器,注意勾选spring web选项
注释掉pom文件中的如下代码:可能会导致打包出现问题,我们打包选择maven clear + install 的方式
1 2 3 4 5 6 7 8 <build > <plugins > <plugin > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-maven-plugin</artifactId > </plugin > </plugins > </build >
1.2. 创建MySDKSource类
这个类是数据的模板,开启自动配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package com.spricoder.mysdk;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "mysdk") public class MySDKSource { private String type; private String rate; public String getType () { return type; } public String getRate () { return rate; } public void setType (String type) { this .type = type; } public void setRate (String rate) { this .rate = rate; } }
1.3. 创建MySDKService类
这个类定义了这个Spring Boot对外提供的两个服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.spricoder.mysdk;public class MySDKService { private MySDKSource mySDKSource; public MySDKService (MySDKSource mySDKSource) { this .mySDKSource = mySDKSource; } public String getType () { return mySDKSource.getType(); } public String getRate () { return mySDKSource.getRate(); } }
1.4. 创建MySDKAutoConfiguration类
还记得刚刚那个报错吗?我们现在来解决这个报错。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package com.spricoder.mysdk;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuration @EnableConfigurationProperties(MySDKSource.class) @ConditionalOnProperty(name = "mysdk.enable", havingValue = "enable") public class MySDKAutoConfiguration { @Autowired private MySDKSource mySDKSource; @Bean @ConditionalOnMissingBean(MySDKSource.class) public MySDKService mySDKService () { return new MySDKService (mySDKSource); } }
1.5. 打包
使用maven clean + install
2. 创建mysdk-test项目
这个项目用来测试刚刚的封装是否成功。
项目结构
2.1. 修改pom文件,添加刚刚打包好的项目
在pom文件中添加
1 2 3 4 5 6 <dependency > <groupId > com.spricoder</groupId > <artifactId > mysdk</artifactId > <version > 0.0.1-SNAPSHOT</version > </dependency >
2.2. 修改MysdkTestApplication文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package com.spricoder.mysdktest;import com.spricoder.mysdk.MySDKService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication @RestController public class MysdkTestApplication { public static void main (String[] args) { SpringApplication.run(MysdkTestApplication.class, args); } @Autowired MySDKService mySDKService; @GetMapping("/mysdk") public String getMySDK () { return mySDKService.getType() + ":" + mySDKService.getRate(); } }
2.3. 测试运行
启动MysdkTestApplication
2.3.1. 报错:找不到Bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 *************************** APPLICATION FAILED TO START ***************************Description :Field mySDKService in com.spricoder.mysdktest.MysdkTestApplication required a bean of type 'com.spricoder.mysdk.MySDKService' that could not be found.The injection point has the following annotations: - @org .springframework.beans.factory .annotation.Autowired (required =true )Action :Consider defining a bean of type 'com.spricoder.mysdk.MySDKService' in your configuration.
没有进行工厂配置,找不到对应的部分
解决方案:在mysdk项目中新建resources/META-INF/spring.factories,内容为:
1 2 org.springframework.boot.autoconfigure.EnableAutoConfiguration =com.spricoder.mysdk.MySDKAutoConfiguration
重新maven clean + install套餐
2.3.2. 报错:没有成功自动装配
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 *************************** APPLICATION FAILED TO START ***************************Description :Field mySDKService in com.spricoder.mysdktest.MysdkTestApplication required a bean of type 'com.spricoder.mysdk.MySDKService' that could not be found.The injection point has the following annotations: - @org .springframework.beans.factory .annotation.Autowired (required =true )The following candidates were found but could not be injected: - Bean method 'mySDKService' in 'MySDKAutoConfiguration' not loaded because @ConditionalOnProperty (mysdk.enable=enable) did not find property 'mysdk.enable' Action :Consider revisiting the entries above or defining a bean of type 'com.spricoder.mysdk.MySDKService' in your configuration.
问题:忘了开启自动配置,并添加信息
在mysdk-test的application.properties中添加配置信息如下
1 2 3 mysdk.enable =enable mysdk.type =下雨 mysdk.rate =0.01
2.3.3. 成功运行效果
在浏览器输入:http://localhost:8080/mysdk
可以得到如下结果
评论