Giggs
Giggs

Reputation: 241

Unable to load Spring MVC Application with Mixing of XML and JavaConfig

I have an old application which is SPRING MVC application with xml based configuration. Due to some reasons i have modified it as Spring MVC application with XML and Java Configuration. I referred below two links

  1. https://www.mkyong.com/spring/spring-mixing-xml-and-javaconfig/

  2. Mixing xml and java config with spring

But it is not working , i guess i am missing something. Please go through the below code:-

1.        web.xml  -- Please refer below the web.xml file.
                            <?xml version="1.0" encoding="UTF-8"?>
                            <web-app xmlns="http://java.sun.com/xml/ns/javaee"
                                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
                                version="3.0">
                                <display-name>spring-tutorial-mvc</display-name>
                                <welcome-file-list>
                                    <welcome-file>index.html</welcome-file>     
                                </welcome-file-list>
                                <context-param>
                                    <param-name>contextClass</param-name>
                                    <param-value>
                                        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
                                    </param-value>
                                </context-param>
                            </web-app>

                    2. mvc-config.xml :- Please refer below the dispacter servlet file.                 
                    <?xml version="1.0" encoding="UTF-8"?>        
                    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
                        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
                            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">        
                        <context:component-scan base-package="com.org.smrs"/>                 
                        <mvc:annotation-driven />       
                        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

                                <property name="prefix" value="/view/"/>
                                <property name="suffix" value=".jsp"/>
                        </bean>         
                        <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>         
                    </beans>

            3. app-ctx.xml - Please refer below the application context file.
            <beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:context="http://www.springframework.org/schema/context"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
                xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
                xsi:schemaLocation=" 
               http://www.springframework.org/schema/beans     
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/context 
               http://www.springframework.org/schema/context/spring-context-3.0.xsd
               http://www.springframework.org/schema/aop 
               http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
               http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx.xsd">
                <context:component-scan base-package="com.mpmvvcl.smrs" />

                <bean id="propertyConfigurer"
                    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
                    p:location="/WEB-INF/local.properties" />

                <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
                    destroy-method="close">
                    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
                    <property name="url" value="jdbc:oracle:thin:@host:port:sid" />
                    <property name="username" value="uname" />
                    <property name="password" value="pwd" />
                </bean>     
            </beans>

        4.AppConfig.java :- The java configuration file to load the configurations
        ----------------                 
        @Configuration
        @ImportResource({ "classpath:mvc-config.xml", "classpath:app-ctx.xml" })
        public class AppConfig {

        }

    5. ConsumerDetailController : Controller class 
    ---------------------------
    @Controller
    @RequestMapping(value = "/user/ca")
    public class ConsumerDetailController {

        // -------------------Retrieve All  
        @RequestMapping(value = "/consumer_list/", method = RequestMethod.GET)
        public ModelAndView getConsDetail() {
            ModelAndView mav = new ModelAndView("smrs/consumer_detail");

            return mav;
        }

    }

    But when I hit url http://localhost:8080/Web/user/ca/consumer_list/  in browser i get 404 page . Please advice. Here i don't want to change to complete java configuration. 

Upvotes: 1

Views: 487

Answers (1)

biiyamn
biiyamn

Reputation: 506

mvc-context.xml should be loaded by the disptacher servlet not by the root application context. remove it from AppConfig import resource. This way

  • the root application context , which is in your case AppConfig , will be loaded by AnnotationConfigWebApplicationContext
  • servlet application context, mvc-context, will be loaded by the dispatcher servlet
  • your web.xml should be something like this. app-ctx.xml and mvc-context.xml if they are under webapp don't use classpath, if they are under resources use classpath prefix

     <web-app>
        <context-param>
           <param-name>contextClass</param-name>
           <param-value>
            org.springframework.web.context.support.
             AnnotationConfigWebApplicationContext
           </param-value>
         </context-param>
       <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>package to .AppConfig</param-value>
       </context-param>
    
       <listener>
        <listener-class>org.springframework.web.context.
        ContextLoaderListener</listener-class>
       </listener>
    
      <servlet>
         <servlet-name>mvc</servlet-name>
         <servlet-class>org.springframework.web.servlet.
             DispatcherServlet</servlet-class>
         <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>mvc-config.xml</param-value>
         </init-param>
          <load-on-startup>1</load-on-startup>
       </servlet>
     <servlet-mapping>
       <servlet-name>mvc</servlet-name>
       <url-pattern>/</url-pattern>
     </servlet-mapping>
    

Upvotes: 2

Related Questions