Listing Scanner in a Web container

public static Class[] scan(ServletContext context) {

String root = context.getRealPath("/WEB-INF/classes"); ClassScanner scanner = new ClassScanner(new File(root)); scanner.setLoader(ClassScanner.class.getClassLoader()); // we pick a specific directory

final File testsDir = new File(root, "com/foo/tests"); scanner.setFilter(new FileFilter() {

public boolean accept(File pathname) {

// check that the file is anywhere inside of test root return pathname.getPath().startsWith(testsDir.getPath());

Class[] classes = scanner.getClasses(); return classes;

In Listing 4-3, context is an instance of ServletContext, obtained from the invoking servlet or JSP page. For performance reasons, it's always a good idea to define a filter that cuts out as many classes as possible. This allows us to reduce the time spent introspecting into each class to discover its annotations (or lack thereof).

0 0

Post a comment

  • Receive news updates via email from this site