当前位置: 首页 > news >正文

@EnableConfigurationProperties @ConfigurationProperties

@EnableConfigurationProperties && @ConfigurationProperties的使用时机

今天在写properties时想到了这个问题,为什么有时候我需要写@EnableConfigurationProperties有时候又不需要呢?下面就详细讲讲。

@Data
@Component
@ConfigurationProperties(prefix = "hm.auth")
public class AuthProperties {private List<String> includePaths;private List<String> excludePaths;
}@Component
@RequiredArgsConstructor
public class AuthGlobalFilter implements GlobalFilter, Ordered {private final AuthProperties authProperties;private final JwtTool jwtTool;private final AntPathMatcher antPathMatcher = new AntPathMatcher();

可以看到AuthGlobalFilter并没有添加@EnableConfigurationProperties,只是简单的注入就可以使用。

@Data
@ConfigurationProperties(prefix = "hm.jwt")
public class JwtProperties {private Resource location;private String password;private String alias;private Duration tokenTTL = Duration.ofMinutes(10);
}@Configuration
@EnableConfigurationProperties(JwtProperties.class)
public class SecurityConfig {@Beanpublic PasswordEncoder passwordEncoder(){return new BCryptPasswordEncoder();}@Beanpublic KeyPair keyPair(JwtProperties properties){// 获取秘钥工厂KeyStoreKeyFactory keyStoreKeyFactory =new KeyStoreKeyFactory(properties.getLocation(),properties.getPassword().toCharArray());//读取钥匙对return keyStoreKeyFactory.getKeyPair(properties.getAlias(),properties.getPassword().toCharArray());}
}

可以看到JwtProperties没有添加@Component注解,也就是没有注册为 Spring 容器中的 bean。然后SecurityConfig中就添加了@EnableConfigurationProperties(JwtProperties.class)然后在下面方法中通过参数自动注入public KeyPair keyPair(JwtProperties properties)


为什么需要 @EnableConfigurationProperties

  • 默认情况下,使用 @ConfigurationProperties 标注的类不会被自动注册为 Spring 容器中的 bean。
  • 使用 @EnableConfigurationProperties(SecurityConfigProperties.class) 会将该类注册为一个 Spring 管理的 bean,使它能够被自动注入。

因为前面JwtProperties没有添加@Component注解,所以需要添加 @EnableConfigurationProperties。而AuthProperties@Component注解,也就是已经被spring管理了,所以不需要额外添加。


简化方式: 如果 SecurityConfigProperties 类本身已经用 @Component 标注,则无需额外使用 @EnableConfigurationProperties

示例:

@Component
@ConfigurationProperties(prefix = "security")
public class SecurityConfigProperties {// 属性和 Getter/Setter 同前
}

在这种情况下,@EnableConfigurationProperties 就变得非必需。


但是如果为了更加稳妥可以把这两个注解全部都加上。@Configuration@Component 都可以使其被spring容器管理。

@Slf4j
@Data
@ConfigurationProperties(prefix = "zzyl.framework.security")
@Configuration
public class SecurityConfigProperties {
}@Configuration
@EnableConfigurationProperties(SecurityConfigProperties.class)
public class SecurityConfig  {@AutowiredSecurityConfigProperties securityConfigProperties;@AutowiredJwtAuthorizationManager jwtAuthorizationManager;
http://www.lryc.cn/news/490919.html

相关文章:

  • RK3588适配MTK7921 USB接口WiFi驱动开发
  • 【数据结构OJ】【图论】图综合练习--拓扑排序
  • 模型 I/O 与 LangChain 实践
  • C++:用红黑树封装map与set-1
  • HBU算法设计与分析 贪心算法
  • python pycharm安装教程及基本使用,超详细
  • 变量提升函数提升
  • el-table vue3统计计算数字
  • IDE应当具备的功能
  • Stable Diffusion初步见解(二)
  • 前端框架 react 性能优化
  • RK3568平台开发系列讲解(Input子系统篇)输入子系统介绍
  • 准备阶段 Profiler性能分析工具的使用(一)
  • go-rod vs Selenium:自动化测试工具的比较与选择
  • 探索免费的Figma中文版:开启高效设计之旅
  • 功能齐全,支持协作 | Docker部署一款支持多人共享的私密浏览器『n.eko』
  • 部署实战(二)--修改jar中的文件并重新打包成jar文件
  • Ubuntu24.04——软件包系统已损坏
  • 2024年华为OD机试真题-空栈压数-C++-OD统一考试(E卷)
  • 嵌入式Linux基于IMX6ULL tslib学习总结
  • go中的参数传递是值传递还是引用传递?
  • 记录一种在内核空间向用户空间通知中断的方法
  • .NetCore 过滤器和拦截器 的区别
  • 【笔记】自动驾驶预测与决策规划_Part7_数据驱动的预测方法
  • React渲染相关内容——渲染流程API、Fragment、渲染相关底层API
  • Python中dict支持多个key的方法
  • 丹摩 | 基于PyTorch的CIFAR-10图像分类实现
  • C#变量和函数如何和unity组件绑定
  • AI模型---安装cuda与cuDNN
  • 【大数据学习 | Spark-Core】Spark提交及运行流程