Closed CXwudi closed 2 years ago
So we have an enablement config with map of PV services to downloader names, but how to validate this map
Q: can we put custom ConstrainValidator
into spring ApplicationContext
and let it has depending bean of Preference
?
A: Yes, a ConstrainValidator
can be a spring component with other dependent components
Q2: can the custom validator contains a bean of Environment
and call checkProperty()
method to validate if a property root path exist?
A: Yes and No, we can only check if the full name property exists, but not the root
About solution for loading only enabled downloaders:
@Conditional(MyCondition.class)
.
MyCondition
with @Component
or using the BeanFactory
in ConditionContext
from the implemented methodCondition
initialized at the very first stage of the Spring life cycle, we can't use any beans. In fact, we will get a null BeanFactory
from ConditionContext
. The only thing we can do is use Environment
from ConditionContext
to check some propertiesenable
and order
. and let all downloaders @ConditionalOnProperty
on their enable
property~ this works if only on enable
property, but how to disable the downloader if pv-preference
doesn't include the target PV service, see #11
@Validated
on downloader class itself and put @field:Valid
on the holding config beanBeanFactory
only, we could try Lazy Initialization
@ConfigurationProperties
with @Validated
been lazily validated and initialized? spring.main.lazy-initialization=true
. Otherwise @ConfigurationProperties
always initialized at startup even if it is @Lazy
. @Bean
s and @Component
s can be lazily loaded.spring.main.lazy-initialization=true
, we can use next optionValidator.validate()
. e.g. inside lazy {}
or init {}
block
Validator
bean will be passed to various business codes. Make things look uglier FInal answer: use @Conditonal(MyCondition.class)
Reason:
RecordListener
listens to all exception)@Conditonal(MyCondition.class)
doesn't affect our project.
when we have more and more downloaders choice in the futures, we can't force users to fill in all downloaders config, so we need:
@ConditionOnBean
to check if the corresponding downloader config exist~BIG NOTICE: base on Spring Doc, Spring Boot issue 1 and issue 2, any@ConditionalOn
annotations that rely on knowledge about Spring beans will NOT work outside of Spring auto-configuration (PS:@ConditionalOnProperty
would probably work)@ConditionOnExpression
with the check if its name is in enablement config map~ Because of this, SpEL can not resolve properties (except@Value
). So we need another solution to load only enabled downloaders