[英]Spring xml config mixed java config
当我像这样配置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>
一切顺利,控制器和视图通过@RequestMapping映射,但是当我添加此空类并构建时
@Configuration
public class Config {
}
路由无效,显示404错误。 那么我必须添加<mvc:annotation-config/>
或@EnableWebMVC
,然后路由就可以了。
有人可以解释一下,我觉得这很奇怪。
编辑:这是我的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.