Spring Boot自定义Starter组件开发实现配置过程

网友投稿 463 2022-07-24


目录自定义starter为什么要自定义starter自定义starter的命名规则实现方法引入依赖编写测试类创建配置类创建spring.factories文件乱码问题解决方案:1. 使用yml配置文件进行配置。2. 使用自定义配置文件如:3. 把中文换成对应的ASCII码。

自定义starter

SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进 starter,应用者只需要在maven中引入starter依赖,SpringBoot就能自动扫描到要加载的信息并启 动相应的默认配置。starter让我们摆脱了各种依赖库的处理,需要配置各种信息的困扰。 SpringBoot会自动通过classpath路径下的类发现需要的Bean,并注册进IOC容器。SpringBoot提供 了针对日常企业应用研发各种场景的spring-boot-starter依赖模块。所有这些依赖模块都遵循着约定 成俗的默认配置,并允许我们调整这些配置,即遵循“约定大于配置”的理念。

为什么要自定义starter

在我们的日常开发工作中,经常会有一些独立于业务之外的配置模块,我们经常将其放到一个特定的 包下,然后如果另一个工程需要复用这块功能的时候,需要将代码硬拷贝到另一个工程,重新集成一 遍,麻烦至极。如果我们将这些可独立于业务代码之外的功能配置模块封装成一个个starter,复用的时 候只需要将其在pom中引用依赖即可,SpringBoot为我们完成自动装配,简直不要太爽。

自定义starter的命名规则

SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建议自定义的starter使用 xxx-spring-boot-starter命名规则。以区分SpringBoot生态提供的starter。

有了以上的了解后,来创建 Maven 项目,目录结构如下:

实现方法

实现自定义starter大致分一下几步:

1.引入pom依赖

2.编写测试用例类

3.创建自动配置类

4.在resources包下增加配置文件

引入依赖

xmlns:xsi="http://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

cn.mystylefree

custom-spring-boot-starter

1.0-SNAPSHOT

17

17

org.springframework.boot

spring-boot-autoconfigure

2.7.0

xmlns:xsi="http://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

cn.mystylefree

custom-spring-boot-starter

1.0-SNAPSHOT

17

17

org.springframework.boot

spring-boot-autoconfigure

2.7.0

这里引入了自动配置类

org.springframework.boot

spring-boot-autoconfigure

2.7knFHFSx.0

编写测试类

在使用Spring官方的Starter时通常可以在application.properties中来配置参数覆盖掉默认的值。即name的值会被配置文件中的值替换掉。

@EnableConfigurationProhttp://perties({PersonProperties.class}) //开启ConfigurationProperties注解

@ConfigurationProperties(prefix = "person")

public class PersonProperties {

private int id;

private String name="章三";

}

省略set/get方法和toString方法

创建配置类

@Configuration

//当类路径classpath下有指定当类 (SimpleBean) 的情况下进行自动配置

@ConditionalOnClass(PersonProperties.class)

public class MyAutoConfiguration {

static {

System.out.println("MyAutoConfiguration init ...");

}

@Bean

public PersonProperties personProperties(){

return new PersonProperties();

}

}

创建spring.factories文件

/META-INF/spring.factories文件放在/src/main/resources目录下注意:META-INF是自己手动创建的目录,spring.factories也是自己手动创建的文件,在该文件中配置自己的自动配置类。

一定要按照下面的位置结构添加

文件中的内容如下

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

cn.mystylefree.config.MyAutoConfiguration

这时配置都加好了

只需从新打下包就行

mvn clean install

我们自己设置的starter包就保存到本地的maven仓库了

接下来我们就能使用刚刚自定义的jar包实现pom依赖引用了

这是我的maven依赖地址

将这个依赖加入到新的项目中引入即可

这是我的一个新springboot项目

1.加入依赖

根据自己的名称添加

cn.mystylefree

custom-spring-boot-starter

1.0-SNAPSHOT

2.设置application.properties

person.id=1

person.name=小米

由于设置的前缀名上person

3.添加测试用例

@SpringBootTest

class SpringBootDemoApplicationTests {

@Autowired

private PersonProperties personProperties;

@Test

void contextLoads() {

int id = personProperties.getId();

String name = personProperties.getName();

System.out.println(id+name);

}

}

这时 调试就能使用自定义的starter了

但是发现出现中文乱吗了,不慌设置一下编码格式

重新启动项目

乱码问题

发现还是乱码???

在Spring Boot项目中,有时候需要自定义一些配置,如果使用中文值并使用注解读取时就会出现乱码。

原因: Spring Boot注解读取application.properties或者application-{profile}.properties文件时默认的是ISO_8859_1编码。

解决方案:

1. 使用yml配置文件进行配置。

Spring Boot在读取yaml配置文件时使用的是UTF-8的编码方式。

2. 使用自定义配置文件如:

custom.properknFHFSxties配置中文属性,并使用@PropertySource(value="classpath:custom.properties", encoding="UTF-8")注解指定读取的文件和编码。代码如下:

@Data

@ConfigurationProperties(prefix = "custom.user")

@PropertySource(value="classpath:custom.properties", encoding="UTF-8")

public class UserProperties {

// @Value("${custom.user.name}")

private String name;

// @Value("${custom.user.sex}")

private String sex;

}

使用@ConfigurationProperties和@Value均可以正常读取。

3. 把中文换成对应的ASCII码。

custom.user.sex=男换成:

custom.user.sex=\u7537以上三种方法均可以正常读取配置文件中的中文字符。

参考文档:

Spring Boot使用@ConfigurationProperties或者@Value读取properties文件中文乱码


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:spring jpa设置多个主键遇到的小坑及解决
下一篇:Java项目实现定时任务的三种方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~