I struggled with this too. The problem is AnnotationConfiguration doesn't scan non-jar classpath url's. The only way I could find to make it work was to subclass AnnotationConfiguration to scan dirs and jars:
Code:
context.setConfigurations (new Configuration []
{
// This is necessary because Jetty out-of-the-box does not scan
// the classpath of your project in Eclipse, so it doesn't find
// your WebAppInitializer.
new AnnotationConfiguration()
{
@Override
public void configure(WebAppContext aContext) throws Exception
{
boolean _metadataComplete = aContext.getMetaData().isMetaDataComplete();
aContext.addDecorator(new AnnotationDecorator(aContext));
AnnotationParser _parser = null;
if (!_metadataComplete)
{
if (aContext.getServletContext().getEffectiveMajorVersion() >= 3 || aContext.isConfigurationDiscovered())
{
_parser = createAnnotationParser();
_parser.registerAnnotationHandler("javax.servlet.annotation.WebServlet", new WebServletAnnotationHandler(aContext));
_parser.registerAnnotationHandler("javax.servlet.annotation.WebFilter", new WebFilterAnnotationHandler(aContext));
_parser.registerAnnotationHandler("javax.servlet.annotation.WebListener", new WebListenerAnnotationHandler(aContext));
}
}
List<ServletContainerInitializer> nonExcludedInitializers = getNonExcludedInitializers(aContext);
_parser = registerServletContainerInitializerAnnotationHandlers(aContext, _parser, nonExcludedInitializers);
if (_parser != null)
{
parse(aContext, _parser);
}
}
private void parse(final WebAppContext aContext, AnnotationParser aParser) throws Exception
{
clearAnnotationList(aParser.getAnnotationHandlers());
List<Resource> _resources = getResources(getClass().getClassLoader());
for (Resource _resource : _resources)
{
if (_resource == null)
return;
ClassNameResolver _resolver = new ClassNameResolver()
{
public boolean isExcluded (String name)
{
if (aContext.isSystemClass(name)) return true;
if (aContext.isServerClass(name)) return false;
return false;
}
public boolean shouldOverride (String name)
{
//looking at webapp classpath, found already-parsed class of same name - did it come from system or duplicate in webapp?
if (aContext.isParentLoaderPriority())
return false;
return true;
}
};
if (_resource.isDirectory())
aParser.parse(_resource, _resolver);
else
aParser.parse(_resource.getURI(), _resolver);
}
//TODO - where to set the annotations discovered from WEB-INF/classes?
List<DiscoveredAnnotation> _annotations = new ArrayList<DiscoveredAnnotation>();
gatherAnnotations(_annotations, aParser.getAnnotationHandlers());
aContext.getMetaData().addDiscoveredAnnotations (_annotations);
}
private List<Resource> getResources(ClassLoader aLoader) throws IOException
{
if (aLoader instanceof URLClassLoader)
{
List<Resource> _result = new ArrayList<Resource>();
URL[] _urls = ((URLClassLoader)aLoader).getURLs();
for (URL _url : _urls)
_result.add(Resource.newResource(_url));
return _result;
}
return Collections.emptyList();
}
}
});
I posted a minimal working project in github: https://github.com/steveliles/jetty-...ring-mvc-noxml