Results 1 to 3 of 3

Thread: WebApplicationInitializer not loaded with embedded Jetty

  1. #1
    Join Date
    Jun 2012
    Posts
    1

    Default WebApplicationInitializer not loaded with embedded Jetty

    Hi

    I am trying to make a spring webmvc app without XML config with embedded jetty.
    But i get this error in the logoutput:
    2012-06-05 10:43:05.742:INFO:/ebc:No Spring WebApplicationInitializer types detected on classpath

    It works fine if i deploy the war file to a standalone jetty instance or run it with maven-jetty-plugin.

    But not when i start jetty with this code:
    Code:
    org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server(8080);
    String dir;
    if (new File("src/main/webapp").exists()) dir = "src/main/webapp";
    else dir = "ebc/src/main/webapp";
    WebAppContext context = new WebAppContext(dir, "/ebc");
    context.setClassLoader(Thread.currentThread().getContextClassLoader());
    context.setResourceBase(".");
    context.setConfigurations (new Configuration []
    {
    	new WebInfConfiguration (),
    	new FragmentConfiguration (),
    	new AnnotationConfiguration (),
    });
    server.setHandler(context);
    server.start();
    server.join();
    Complete test project can be downloaded : http://dl.dropbox.com/u/331760/test-jetty.tar.bz2
    Hope someone can help me with this.

  2. #2
    Join Date
    Sep 2012
    Posts
    1

    Default

    I have same problem.
    Does someone have any solution?

  3. #3
    Join Date
    Nov 2012
    Posts
    1

    Default

    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •