一、准备部分
- 开发工具: IDEA
- 注意事项:官方的start都是spring-boot-starter-xxx,非官方的建议是以xxx-spring-boot-starter命名
二、创建项目
1. pom引入的依赖
<!-- 引入依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 增加configuration -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- -->
<configuration>
<!-- 启动类的路径-->
<mainClass>com.youchuan.test.TestApplication</mainClass>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
2. 项目结构
3. 新建一个TestProperties在properties文件夹下
@ConfigurationProperties 这是关键的注释
@ConfigurationProperties(prefix = "test")
public class TestProperties {
private String name;
private String words;
private String toWho;
// 省略getter和setter
}
4. 新建一个TestService在service文件夹下
public class TestService {
private String name;
private String word;
private String toWho;
// 实现简单的业务: 字符串拼接
public String OutputOnView() {
return getName() + "say: " + getWord() + "to ---> " + getToWho();
}
// 省略getter和setter
}
5. 新建一个TestAutoConfiguration在config文件夹下
使用注解 @Configuration 配合 @Bean 注册一个拼接字符串的bean对象
@EnableConfigurationProperties(TestProperties.class) 启用 TestProperties 配置类
@Configuration
@EnableConfigurationProperties(TestProperties.class)
public class TestAutoConfiguration {
@Autowired
private TestProperties testProperties;
@Bean
public TestService testService(){
TestService service = new TestService();
service.setName(testProperties.getName());
service.setToWho(testProperties.getToWho());
service.setWord(testProperties.getWords());
return service;
}
}
6.最关键的部分: 在META-INF创建spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.youchuan.test.config.TestAutoConfiguration
三、导出jar包
在终端输入mvn install打jar包
在项目文件可以找到打好的jar包
四、 新建另一个项目demo进行测试
- 添加
test自定义的jar包
<!--省略springboot工程常规依赖-->
<dependency>
<groupId>com.youchuan</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
- 在yml文件中添加配置
test:
name: youchuan
words: hello mother f**k
to-who: Mawangcai
或者properties文件也行
test.name=youchuan
test.words=hello mother f**k
test.to-who=Mawangcai
- 添加测试类
@RestController
public class TestMyJar {
@Autowired
private TestService testService;
@GetMapping("/test")
public String test() {
String str = testService.OutputOnView();
return str;
}
}
- 查看
五、进阶
来试试@ConditionalOnProperty
prefix 配置属性名称的前缀
name 配置属性完整名称或部分名称
havingValue 比较获取到的属性值与havingValue给定的值是否相同,相同才加载配置
在TestAutoConfiguration文件上进行添加
@Configuration
@EnableConfigurationProperties(TestProperties.class)
// ========================================增加部分=====================================
@ConditionalOnProperty(
// 配置属性名称的前缀,比如spring.http.encoding
prefix = "test",
// 配置属性完整名称或部分名称
// 可与prefix组合使用,组成完整的配置属性名称,与value不可同时使用
name = "isopen",
// 比较获取到的属性值与havingValue给定的值是否相同,相同才加载配置
havingValue = "true"
)
// =====================================================================================
public class TestAutoConfiguration {
@Autowired
private TestProperties testProperties;
@Bean
public TestService testService() {
TestService service = new TestService();
service.setName(testProperties.getName());
service.setToWho(testProperties.getToWho());
service.setWord(testProperties.getWords());
return service;
}
}
yml文件中新增isopen属性即可
test:
name: youchuan
words: hello mother f**k
to-who: Mawangcai
isopen: true
因为前边我们设定havingValue等于true才开始配置,所以如果isopen不为true那么配置就不会加就会报错。