admin管理员组文章数量:1415145
I would like to write an ArchUnit rule which check that classes annotated with @Configuration in Spring does not have any fields that are PUBLIC.
For instance, this class should be OK, there is no field at all:
@Configuration
@EnableWebSecurity
class SecurityConfiguration {
@Bean
SecurityFilterChain securityFilterChain(final HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeHttpRequests(authorize -> authorize.anyRequest().permitAll()).csrf(AbstractHttpConfigurer::disable);
return httpSecurity.build();
}
}
This class should also be OK; there is a field, but it is private static final:
@Configuration
class JdkClientHttpRequestFactoryConfiguration {
private static final int DEFAULT_READ_TIMEOUT = 5;
@Bean
JdkClientHttpRequestFactory jdkClientHttpRequestFactory() {
final JdkClientHttpRequestFactory jdkClientHttpRequestFactory = new JdkClientHttpRequestFactory();
jdkClientHttpRequestFactory.setReadTimeout(Duration.ofSeconds(DEFAULT_READ_TIMEOUT));
return jdkClientHttpRequestFactory;
}
}
And fail if any public fields.
I tried this rule:
@Test
void fieldShouldAllBePrivateToProtectEncapsulation() {
fields().that(areNotConfiguration())
.and().areNotStatic()
.and().areNotFinal()
.should().bePrivate()
.check(importedClasses);
}
private DescribedPredicate<? super JavaField> areNotConfiguration() {
return new DescribedPredicate<>("are not @Configuration") {
@Override
public boolean test(JavaField javaField) {
JavaClass owner = javaField.getOwner();
return !owner.isAnnotatedWith(Configuration.class) || !javaField.getRawType().isAssignableTo(owner.reflect());
}
};
}
However, it yields this very strange kind of failure:
JdkClientHttpRequestFactoryConfiguration$$SpringCGLIB$$0.$$beanFactory> does not have modifier PRIVATE in (:0)
SecurityConfiguration$$SpringCGLIB$$FastClass$$1.$$beanFactory> does not have modifier PRIVATE in (:0)
May I ask how to properly write the ArchUnit rule for classes with @Configuration?
本文标签:
版权声明:本文标题:java - Solve error Field <com.JdkClientHttpRequestFactoryConfiguration$$SpringCGLIB$$0.$$beanFactory> does not hav 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745214610a2648088.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论