سؤال

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

<context:annotation-config/>

I read from the official documentation:

The implicitly registered post-processors include AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor, as well as the aforementioned RequiredAnnotationBeanPostProcessor.

But I was wondering how Spring does this work under the hood, I would think that this 1-liner is converted into several bean definitions for the post-processors mentioned by the documentation.

However, my question is, which Spring component/class implements this 'conversion from 1-liner to multiple bean definitions' functionality?

هل كانت مفيدة؟

المحلول

If you want to know what the annotation-config tag does behind the scenes look into the AnnotationConfigBeanDefinitionParser

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

You need a schema definition, a NameSpaceHandler and a BeanDefinitionParser

نصائح أخرى

Good references by gkamal.

What happens is that Spring registers all these custom namespaces when the app starts up, and namespace-specific handlers register parsers for each element in the custom namespace. Here for example is a custom namespace I did in one of my own projects:

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

Notice that all the NamespaceHandler does is register a bunch of parsers for the custom namespace.

Then when Spring actually parses the config file, it turns the custom namespace element into bean definitions as you suggest in your question. Sometimes it's just a very simple bean definition:

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

and sometimes it's a whole bunch of bean definitions:

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

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

The property defined are resolved at start-up time.

The placeholders follows this style ${property-name}

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top