无法加载混合了 XML 和 JavaConfig 的 Spring MVC 应用程序

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

我有一个旧应用程序,它是基于 xml 配置的 SPRING MVC 应用程序。由于某些原因,我将其修改为具有 XML 和 Java 配置的 Spring MVC 应用程序。 我在下面提到了两个链接

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

  2. Mixing xml and java config with spring

但它不起作用,我想我遗漏了什么。请通过以下代码:-

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. 

mvc-context.xml 应该由调度程序 servlet 加载,而不是由根应用程序上下文加载。从 AppConfig 导入资源中删除它。这样

  • 根应用程序上下文(在您的情况下为 AppConfig)将由 AnnotationConfigWebApplicationContext 加载
  • servlet 应用程序上下文 mvc-context 将由调度程序 servlet 加载
  • 你的web.xml应该是这样的。 app-ctx.xml 和 mvc-context.xml 如果它们在 webapp 下则不使用类路径,如果它们在资源下则使用类路径前缀

     <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>