2

As Google App Engine will start and stop instances regularly, and this means incurring the cold start time regularly, I'd like to configure my Spring MVC3 app using XML to avoid the 3-5 sec delay caused by scanning the class files for annotations when using annotation configuration when a new instance is spun up.

However writing the xml is a bit of a chore and much easier to use the annotations to define my configuration. So I'd like the best of both worlds and to use the annotations to generate the config file, and then turn off the scanning at runtime. From this question it seems there aren't any existing tools that will do this.

So what is the best way to approach this? Presumably there is a class which does the scanning in spring at runtime that could be re-appropriated to scan at design time and then spit out the xml?

Are there any limitations on things which can be done from the annotation configuration which will not be possible in the xml configuration?

1 Answer 1

1

I would do this using Spring for scanning the package that contains the annotated classes, then using reflection for getting the annotations on the class and its methods and writing the XML accordingly to them.

The class that does the scanning in Spring is ClassPathScanningCandidateComponentProvider. Here is a code snippet of how it can be used :

ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(Component.class)); 
for(String packageToScan : packagesToScan) {
    for (BeanDefinition bd : scanner.findCandidateComponents(packageToScan)) {
        Class clazz = Class.forName(bd.getBeanClassName());
        // Use reflection on clazz to write the XML file
    }
}

I hope this helps !

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Felix. I'd hoped there was a class which actually scanned for all of the correct annotations already and generated some object which is also generated by parsing the xml, and I would be able to take the results of the annotation scanning version and turn its objects into the xml required to satisfy the xml version. I'll have a look at the class you suggest though and see where it gets me.
looks like ClassPathBeanDefinitionScanner might give me what I want
You are right, this looks more accurate. Thanks for the info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.