Announcement Announcement Module
Collapse
No announcement yet.
Java based config NOT firing up the config class off WebApplicationInitializer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Java based config NOT firing up the config class off WebApplicationInitializer

    Hello,

    I have a Maven project that I'm able to run in a Tomcat container using the command: mvn clean tomcat:run -Pmysql-db if the configuration is stored in the web.xml file.

    Now, if I remove this file and use a Java based configuration instead, the application is not started.

    I've upgraded to Tomcat 7:
    Code:
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.2</version>
            <configuration>
              <!-- <webXml>src/main/webapp/WEB-INF/web.xml</webXml> -->
              <failonmissingwebxml>false</failonmissingwebxml>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.1</version>
          </plugin>
        </plugins>
    I'm using the Sevlet 3:
    Code:
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.0.1</version>
          <scope>provided</scope>
        </dependency>
    I'm using Spring 3.2.2.RELEASE:

    Here is the class web initialization class:
    Code:
    public class WebInit implements WebApplicationInitializer {
    
    	private static Logger logger = LoggerFactory.getLogger(WebInit.class);
    
    	@Override
    	public void onStartup(ServletContext servletContext) throws ServletException {
    		logger.debug("================>>> OKAY Web Init");  // NOT seen in the log
    		
    		// Create the root application context
    		AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
    
    		// Set the application display name
    		appContext.setDisplayName("Spring Web Demo");
    
    		// Register the application configuration with the root context
    		appContext.register(ApplicationConfiguration.class);
    
    		// Create the Spring Container shared by all servlets and filters
    		servletContext.addListener(new ContextLoaderListener(appContext));
    		
    		// Create the dispatcher servlet context
    		AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext();
    
    		// Register the servlet configuraton with the dispatcher servlet context
    		webApplicationContext.register(ApplicationConfiguration.class);
    
    		// Add a servlet
    		ServletRegistration.Dynamic learnintouchServlet = servletContext.addServlet("learnintouch", new DispatcherServlet(webApplicationContext));
    		learnintouchServlet.setLoadOnStartup(1);
    		learnintouchServlet.addMapping("/");
    	}
    	 
    }
    And the application configuration class:
    Code:
    @Configuration
    @ImportResource({ "classpath:jpa.xml", "classpath:data-source.xml",	"classpath:service-jpa.xml", "classpath:log4j.xml" })
    @Import(WebConfiguration.class)
    public class ApplicationConfiguration extends WebMvcConfigurerAdapter {
    
    	@Bean
    	public static PropertySourcesPlaceholderConfigurer properties() {
    		return new PropertySourcesPlaceholderConfigurer();
    	}
    
    	@Override
    	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    		// equivalent to <mvc:argument-resolvers>
    	}
    
    	@Override
    	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    		// equivalent to <mvc:message-converters>
    	}
    
    	@Bean
    	public ServletWebArgumentResolverAdapter resolver() {
    		return new ServletWebArgumentResolverAdapter(pageable());
    	}
    
    	@Bean
    	public PageableArgumentResolver pageable() {
    		return new PageableArgumentResolver();
    	}
    	  
    }
    And the web configuration class:
    Code:
    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = "com.thalasoft.learnintouch.web.controller")
    public class WebConfiguration extends WebMvcConfigurerAdapter {
    
    	@Bean
    	public UrlBasedViewResolver urlBasedViewResolver() {
    		UrlBasedViewResolver urlBasedViewResolver = new InternalResourceViewResolver();
    		urlBasedViewResolver.setViewClass(JstlView.class);
    		urlBasedViewResolver.setPrefix("/WEB-INF/jsp/");
    		urlBasedViewResolver.setSuffix(".jsp");
    		return urlBasedViewResolver;
    	}
    
    	@Override
    	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    		PageableArgumentResolver resolver = new PageableArgumentResolver();
    		resolver.setFallbackPagable(new PageRequest(1, 10));
    		argumentResolvers.add(new ServletWebArgumentResolverAdapter(resolver));
    		super.addArgumentResolvers(argumentResolvers);
    	}
    	
    }
    The console shows only;
    [INFO] Running war on http://localhost:8080/learnintouch-web
    [INFO] Creating Tomcat server configuration at /home/stephane/dev/java/projects/learnintouch/web/target/tomcat
    Jun 12, 2013 5:20:17 PM org.apache.catalina.startup.Embedded start
    INFO: Starting tomcat server
    Jun 12, 2013 5:20:18 PM org.apache.catalina.core.StandardEngine start
    INFO: Starting Servlet Engine: Apache Tomcat/6.0.29
    Jun 12, 2013 5:20:19 PM org.apache.coyote.http11.Http11Protocol init
    INFO: Initializing Coyote HTTP/1.1 on http-8080
    Jun 12, 2013 5:20:19 PM org.apache.coyote.http11.Http11Protocol start
    INFO: Starting Coyote HTTP/1.1 on http-8080
    Any clue on what I could be missing ?

    Thanks.

  • #2
    I can now see my WebInit classfiring up when using the command:

    mvn clean org.apache.tomcat.maven:tomcat7-maven-plugin:2.1:run -Pmysql-db

    Comment


    • #3
      After adding a goal prefix and moving the plugin from the child module pom.xml file to the parent pom.xml file

      Code:
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <goalPrefix>tomcat7</goalPrefix>
        </configuration>
      </plugin>
      I can now run it with the following command:

      mvn clean tomcat7:run -Pmysql-db

      The mavenwar plugin also had to be slightly altered:
      Code:
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-war-plugin</artifactId>
              <version>2.2</version>
              <configuration>
                <!-- <webXml>src/main/webapp/WEB-INF/web.xml</webXml> -->
                <failonmissingwebxml>false</failonmissingwebxml>
              </configuration>
            </plugin>
      Last edited by stephaneeybert; Jun 14th, 2013, 07:35 AM.

      Comment

      Working...
      X