简体   繁体   English

Spring XML配置混合Java配置

[英]Spring xml config mixed java config

I have created a basic Spring MVC app, when I config xml like this 当我像这样配置xml时,我已经创建了一个基本的Spring MVC应用程序

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-4.3.xsd
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
" >
    <context:component-scan base-package="com.hell"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

everything goes well, the controller and the view mapped via @RequestMapping, but when I add this empty class and build 一切顺利,控制器和视图通过@RequestMapping映射,但是当我添加此空类并构建时

@Configuration
public class Config {
}

the routing does not work, it say 404 error. 路由无效,显示404错误。 then I must add <mvc:annotation-config/> or @EnableWebMVC , the routing then works. 那么我必须添加<mvc:annotation-config/>@EnableWebMVC ,然后路由就可以了。
can someone explain this, I think this is so weird. 有人可以解释一下,我觉得这很奇怪。
edit: this is my web.xml to config DispatcherServlet 编辑:这是我的Web.xml来配置DispatcherServlet

<servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

我已经搜索了Internet,发现context:component-scan告诉Spring扫描@Configuration类,所以也许这是我的问题的答案,任何人都请纠正我。

@Configuration用于基于Java的spring配置,所以我认为,如果您正在通过Java代码对bean进行配置,则您的xml配置受到限制。我建议您坚持一种方法,这将使您的代码易于维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM