Listing annotated classes in Java EE

The Java EE platform is huge, and I must confess I’m not aware of every nook and cranny of its recent API. Lately, I learned a new trick.

I was working on the version 10 of the Vaadin framework also known as Vaadin Flow. This version introduces routes: a route is a path/component pair. When a path is requested, the Vaadin Servlet displays the component. Routes are created by annotating specific components with the @Route annotation. Thus, Vaadin needs to create route objects at startup time. And for that, it needs to list all classes annotated with @Route.

To achieve that, Java EE offers the following:


This annotation lists all other annotation classes the container must look for.


Implementations of this interface will be scanned by the platform. It must be annotated by the former annotation, with the list of annotations to look for. Then, its onStartup(c: Set<Class<?>>, ctx: ServletContext) method will be invoked by the platform. The set will be filled with classes that were found.

Vaadin makes use of it like this:

class diagram

Here is a code excerpt:

@HandlesTypes({ Route.class, RouteAlias.class })
public class RouteRegistryInitializer extends AbstractRouteRegistryInitializer
        implements ServletContainerInitializer {

    public void onStartup(Set<Class<?>> classSet, ServletContext servletContext)
            throws ServletException {
         // Redacted for brevity's sake

The platform will call the onStartup() method, and the classSet will be filled with every class annotated with either @Route or @RouteAlias.

Nicolas Fränkel

Nicolas Fränkel

Nicolas Fränkel is a Developer Advocate with 15+ years experience consulting for many different customers, in a wide range of contexts (such as telecoms, banking, insurances, large retail and public sector). Usually working on Java/Java EE and Spring technologies, but with focused interests like Rich Internet Applications, Testing, CI/CD and DevOps. Currently working for Exoscale. Also double as a teacher in universities and higher education schools, a trainer and triples as a book author.

Read More