"Manually" by creating beans using Grails BeanBuilder or Spring GenericGroovyApplicationContext

I have a class with complex creation logic (uses a builder for example). Back in 2000, since XML is rigid and not a programming language, I couldn't code the creation logic in it, so I encapsulated it in a FactoryBean.

Then the blessed JavaConfig came in (thanks @cbeams) and threw the FactoryBean into the history trash.

Since GroovyConfig is an extra step forward (not only a real programming language for configuration, but also a DSL), I was confident that I would find an easy and elegant way to code my path, although the creation logic is complex, but not find any mention of the ability to do this is?!

I understand that GroovyConfig is more or less taken verbatim from Grails BeanBuilder, so maybe if there is a way to do this it will also work in GroovyConfig (fingers crossed).

Please tell me that I am missing something obvious and do not need to use FactoryBean again!

Sleeping on it, I think the answer is no. I am adding an answer (still hoping it would be horribly omitted as wrong). Please prove that I am wrong!


source to share

1 answer

Thinking about it, it looks like the answer is no. It looks like I can't do without FactoryBean

, so:

  • XML and GroovyConfig - BeanDefinitionReader

    s. They parse configuration files (XML and groovy script respectively) and build BeanDefinition

    from them. Any logic I code in a groovy script affects the BeanDefinition (for example, I can wrap a region in if-else

    ). Then, in a later phase that I have no control over, Spring creates the beans from the definition itself.
  • JavaConfig is different. It is not parsed as a config file to create BeanDefinition

    , but instead the objects created in it are the beans themselves (and BeanDefinition

    with them)! This means that I have control over the bean creation time and can implement any bean instantiation logic without FactoryBean

    , not just the BeanDefinition



All Articles