Giter VIP home page Giter VIP logo

Comments (3)

CXwudi avatar CXwudi commented on June 8, 2024

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

from vocadb-video-downloader-new.

CXwudi avatar CXwudi commented on June 8, 2024

About solution for loading only enabled downloaders:

  • use @Conditional(MyCondition.class).
    • Try either annotate MyCondition with @Component or using the BeanFactory in ConditionContext from the implemented method
    • An: since Condition 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 properties
  • let all downloader configs have two common properties, enable 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
    • validation on downloader config will be fully working as the user may disable the downloader you can put @Validated on downloader class itself and put @field:Valid on the holding config bean
  • Spring Lazy Initialization. Since all downloaders are getten through BeanFactory only, we could try Lazy Initialization
    • can @ConfigurationProperties with @Validated been lazily validated and initialized?
    • An: Yes, but must enable global lazy initialization by spring.main.lazy-initialization=true. Otherwise @ConfigurationProperties always initialized at startup even if it is @Lazy. @Beans and @Components can be lazily loaded.
    • If don't want spring.main.lazy-initialization=true, we can use next option
  • call Validator.validate(). e.g. inside lazy {} or init {} block
    • is an easy understand solution, but Validator bean will be passed to various business codes. Make things look uglier

from vocadb-video-downloader-new.

CXwudi avatar CXwudi commented on June 8, 2024

FInal answer: use @Conditonal(MyCondition.class)
Reason:

  • eager initialization is still needed so that no config validation failure during the business logic part (remember that RecordListener listens to all exception)
  • good isolation of validation code and business code, although this method led to more code to write
  • very very very, fortunately, all our condition is based on configuration properties, so the limitation of using @Conditonal(MyCondition.class) doesn't affect our project.
    • but this might not be the case in future projects, consider using Micronauts framework or POC creating our own auto-configuration classes (this is for writing libraries)

from vocadb-video-downloader-new.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.