企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
案例代码:https://gitee.com/flymini/codes01/tree/master/springboot_/com-learn-boot04 **** [TOC] # 1. 参考HttpEncodingAutoConfiguration 在自定义自动配置前,我们先参考一下 HttpEncodingAutoConfiguration 自动配置,看一下实现一个自动配置类基本需要哪些注解,它的部分源码如下: ``` // 读取配置文件的类 @Configuration( proxyBeanMethods = false ) // 将ServerProperties.class加载到当前的类中 @EnableConfigurationProperties({ServerProperties.class}) // 判断当前的项目是否为Web项目,是则让当前的配置类生效 @ConditionalOnWebApplication( type = Type.SERVLET ) // 判断CharacterEncodingFilter.class是否存在,如果存在则加载到当前类中 @ConditionalOnClass({CharacterEncodingFilter.class}) // 读取配置文件中以server.servlet.encoding为前缀的属性的属性值 // 该属性的默认值为enabled // matchIfMissing=true 不管配置文件中是否存在prefix,一律让 // 当前配置类生效 @ConditionalOnProperty( prefix = "server.servlet.encoding", value = {"enabled"}, matchIfMissing = true ) // 设置编码的核心类 public class HttpEncodingAutoConfiguration { private final Encoding properties; ... // 将方法的返回值作为<bean class="返回的返回值"/> // 将方法名作为<bean id="方法名"/> @Bean // 如果CharacterEncodingFilter没有被注入Spring的IoC容器,则注入 @ConditionalOnMissingBean public CharacterEncodingFilter characterEncodingFilter() { CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter(); filter.setEncoding(this.properties.getCharset().name()); filter.setForceRequestEncoding(this.properties.shouldForce(org.springframework.boot.web.servlet.server.Encoding.Type.REQUEST)); filter.setForceResponseEncoding(this.properties.shouldForce(org.springframework.boot.web.servlet.server.Encoding.Type.RESPONSE)); return filter; } ``` <br/> # 2. 自定义自动配置 下面自定义自动配置类 CustomAutoConfiguration,它将 AccountServiceImpl 组件自动注册到IoC容器中。 <br/> 步骤如下: **1. 引入 spring-boot-autoconfigure 依赖** *`pom.xml`* ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.3.12.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> </dependency> </dependencies> ``` **2. 用于读取配置文件的properties类** ```java @Data @Component //当IoC容器中存在多个CustomProperties时会产生冲突,@Primary表示将当前类设置为 //默认的,这样可以防止冲突 @Primary @ConfigurationProperties(prefix = "custom.account") public class CustomProperties { private String name; private String password; } ``` **3. AccountServiceImpl组件** ```java @Data public class AccountServiceImpl { private CustomProperties properties; } ``` **4. 定义自动配置类** ```java @Primary @Configuration //将当前类作为IoC容器 @EnableConfigurationProperties(CustomProperties.class) //启用CustomProperties @ConditionalOnClass(CustomProperties.class) //当CustomProperties存在时当前类才生效 //配置文件中存在前缀为 custom.account 的配置时当前类才生效 @ConditionalOnProperty(prefix = "custom.account", value = "enabled", matchIfMissing = true) public class CustomAutoConfiguration { @Autowired private CustomProperties properties; @Bean //注册组件AccountServiceImpl @ConditionalOnMissingBean //当组件AccountServiceImpl还没有被注册时才注册 public AccountServiceImpl accountService() { AccountServiceImpl impl = new AccountServiceImpl(); impl.setProperties(properties); return impl; } } ``` **5. 将自动配置类加入到`resources/META-INF/spring.factories`文件中** ```factories # Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.learn.boot04.config.CustomAutoConfiguration ``` **6. 在`resources/application.yml`配置文件中添加`custom.account`前缀的配置** ```yml custom: account: name: zhangsan password: 123456 ``` **7. 测试** ```java @SpringBootTest public class Boot04ApplicationTests { @Autowired private AccountServiceImpl accountService; @Test public void contextLoads() { CustomProperties properties = accountService.getProperties(); System.out.println(properties); //CustomProperties(name=zhangsan, password=123456) } } ```