As an example, I will map class attribute name to xml tag name.
The code can be grabbed from my github account.
I defined two annotations:
XmlMapping to tag classes I want to scan and XmlFieldMapping to store metadata. Once those annotations are defined, I am able to store metadata inside my classes.Let's write a class that will extract metadata contained inside an annotated class:
Extractor class iterates over each attribute and extracts metadata. Those information are stored in
XmlMetadata. Doing so, we are caching those information and this reflexion code will be executed only once.The class path scanner will scan a directory looking for classes containing
XmlMapping annotation. All found classes will be passed to the extractor in order to store metadata contained inside XmlMetadata.In order to scan the package, we use a spring class named
ClassPathScanningCandidateComponentProvider. We can construct it using true the default configuration. This default configuration will find any classe defining a spring bean. Here we pass false and pass XmlMapping annotation as a filter. The init() method is called when spring boot using the init-method attribute in spring configuration.