软件编程
位置:首页>> 软件编程>> java编程>> Spring Boot自定义Starter组件开发实现配置过程

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

作者:奇点_97  发布时间:2022-05-31 16:07:12 

标签:Spring,Boot,Starter,组件,开发

自定义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 项目,目录结构如下:

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

实现方法

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

1.引入pom依赖

2.编写测试用例类

3.创建自动配置类

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

引入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        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">
   <modelVersion>4.0.0</modelVersion>
   <groupId>cn.mystylefree</groupId>
   <artifactId>custom-spring-boot-starter</artifactId>
   <version>1.0-SNAPSHOT</version>
   <properties>
       <maven.compiler.source>17</maven.compiler.source>
       <maven.compiler.target>17</maven.compiler.target>
   </properties>
   <dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-autoconfigure</artifactId>
           <version>2.7.0</version>
       </dependency>
   </dependencies>
</project>

这里引入了自动配置类

<dependencies>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-autoconfigure</artifactId>
           <version>2.7.0</version>
       </dependency>
   </dependencies>

编写测试类

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

@EnableConfigurationProperties({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也是自己手动创建的文件,在该文件中配置自己的自动配置类。

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

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

文件中的内容如下

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.mystylefree.config.MyAutoConfiguration

这时配置都加好了

只需从新打下包就行

mvn clean install

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

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

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

这是我的maven依赖地址

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

这是我的一个新springboot项目

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

1.加入依赖

根据自己的名称添加

<dependency>
           <groupId>cn.mystylefree</groupId>
           <artifactId>custom-spring-boot-starter</artifactId>
           <version>1.0-SNAPSHOT</version>
       </dependency>

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自定义Starter组件开发实现配置过程

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

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

重新启动项目

乱码问题

发现还是乱码???

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

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

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

解决方案:

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

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

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

custom.properties配置中文属性,并使用@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文件中文乱码

来源:https://blog.csdn.net/qq_29235677/article/details/125226948

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com