[英]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.