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

泰安企业建站公司淘宝关键词搜索量查询工具

泰安企业建站公司,淘宝关键词搜索量查询工具,ui作品集 网站怎么做,抖音小程序注册EnableConfigurationProperties && ConfigurationProperties的使用时机 今天在写properties时想到了这个问题,为什么有时候我需要写EnableConfigurationProperties有时候又不需要呢?下面就详细讲讲。 Data Component ConfigurationProperties(pr…

@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.yayakq.cn/news/827228/

相关文章:

  • 手机购物网站建设怎么弄一个电商平台
  • 单产品网站模板湖北省麻城建设局官方网站
  • 外贸企业网站建设项目投资网
  • 做网站优势网站开发合同要上印花税吗
  • idea做网站杭州物联网前十名公司
  • 建立网站ftp是什么有哪里可以做兼职翻译的网站
  • 解答网站内容优化策略樱花jsq30q211
  • 济南智能网站建设流程网络开发公司
  • 一个网站是如何建设王也最后结局
  • 专业网站网站建设项目建议书
  • 网站建设及维护价钱开个做网站的公司 知乎
  • 长沙做网站咨询公司昆明建设工程质量备案在哪个网站
  • 扬州做公司网站微信商城分销系统方案
  • 吸引流量的网站风景网页制作模板
  • wordpress教程linux绍兴seo网站推广
  • 如何做家乡网站河源网站页面优化ppt
  • 门户网站建设情况汇报宁波建设商城网站
  • 14年网站开发经验广州网站制作实力乐云seo
  • 可以做qq空间背景音乐的网站关于网站制作的评价
  • 小企业网站源码莲湖微网站建设
  • 做pc端网站咨询百度搜索推广技巧
  • 站长工具友链查询wordpress如何设置边栏
  • 烟台网站建设哪家好呢网页设计html代码教程
  • 贵阳专业网站建设公司哪家好wordpress怎么安装上服务器
  • 南充建设机械网站有哪些做壁纸的网站
  • 博物馆网站做的好的12306网站建设费用
  • 在线短视频网站开发费用前山网站建设
  • 网站建设整合营销怎么去创建一个网站
  • 动漫网站开发 百度一下股权众筹网站开发
  • 贵阳网站建设公设计一个网站的首页步骤