How does Spring contextual namespace work?

For example, if I declare in my application-context.xml:

<context:annotation-config/> 

I read the official documentation:

Implicitly registered postprocessors include AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor, as well as the aforementioned RequiredAnnotationBeanPostProcessor.

But I was wondering how Spring works under the hood, I think this 1-liner will be converted to several bean definitions for the post-processors mentioned in the documentation.

However, my question is that Spring component / class implements this "conversion from 1-line to multiple bean functionality?"

+4
source share
3 answers

If you want to know what the annotation-config tag does behind the scenes, check out AnnotationConfigBeanDefinitionParser

If you want to learn more about the general mechanism used to define such tags, check out this section of the spring reference documentation.

You need a schema definition, NameSpaceHandler and BeanDefinitionParser

+5
source

Good links on gkamal.

What happens is that Spring registers all these user namespaces when the application starts, and handlers for specific namespaces register parsers for each element in the user namespace. Here, for example, is my own namespace, which I made in one of my own projects:

https://github.com/williewheeler/kite/blob/master/src/main/java/org/zkybase/kite/config/xml/KiteNamespaceHandler.java

Note that all NamespaceHandler does indeed register a bunch of parsers for the user namespace.

Then, when Spring actually parses the configuration file, it turns the user namespace element into bean definitions, as you suggest in your question. Sometimes this is just a very simple definition of a bean:

https://github.com/williewheeler/kite/blob/master/src/main/java/org/zkybase/kite/config/xml/CircuitBreakerParser.java

and sometimes this is a whole group of bean definitions:

https://github.com/williewheeler/kite/blob/master/src/main/java/org/zkybase/kite/config/xml/AnnotationConfigParser.java

+1
source

It helps externalize property values ​​from a bean definition in a separate file.

Defined properties are resolved at startup.

Placeholders follow this style of $ {property-name}

0
source

All Articles