我一直在編寫控制器擴展控制器類的項目。我是否可以在同一個應用程序中配置和使用基於POJO的控制器(使用@Controller)?在spring 2.5中可以使用基於註釋和基於xml的配置嗎?
非常感謝
我一直在編寫控制器擴展控制器類的項目。我是否可以在同一個應用程序中配置和使用基於POJO的控制器(使用@Controller)?在spring 2.5中可以使用基於註釋和基於xml的配置嗎?
非常感謝
感謝jamestastic和skaffman,其現在的工作都很好:)
下面是需要線被addeded到Web配置文件,讓他們一起工作:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" ...line1
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context ...line2
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> ...line3
<context:annotation-config/> ...line4
<context:component-scan base-package="myPackage"/> ...line5
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> ...line6
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> ...line7
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> ...line8
</beans>
我太懶惰在我的主應用程序中不添加第8行。
非常感謝
絕對。您可以根據您的選擇將它們混合在一起。 DispatcherServlet
應該在同一個應用程序中同時識別舊式和新式控制器。
是的,你可以。您需要包含Spring JavaConfig項目庫,因爲註釋配置不是2.5核心的一部分。
Here is an example我寫了一段比較Google Guice和Spring的比較。接近底部(查找@ImportXml),我將展示如何將Spring XML配置與註釋配置相結合。配置如下所示:
@Configuration
@ImportXml(locations = "classpath:com/earldouglas/guicespringjc/spring/config.xml")
public class XmlSpringConfiguration {
}
請參閱Spring Reference regarding combining XML and Annotation configuration。這是來自Spring 3的文檔,但它仍然適用(可能對舊的Spring JavaConfig項目的類名和路徑進行較小的更改)。
我想你可能誤解了這個問題。他指的是舊式控制器和新式註釋控制器,而不是「@ Bean」式配置。無可否認,該主題確實另有建議。 – skaffman 2010-09-07 16:17:26
你是對的,我誤解了這個問題。我會在這裏留下我的答案,以防其他人碰巧發現它有用。 – earldouglas 2010-09-07 16:19:42
在春天> = 3.0使用@ImportResource註釋
@Configuration
@ImportResource({ "classpath:/path/to/spring.xml", })
public class AppConfig {
}
非常感謝skaffman :)。你能指導我參考一下,我可以找到如何配置我的應用程序,所以我可以將它們兩個一起使用? – skip 2010-09-07 15:58:55
@skip:你不需要,它只是工作。 – skaffman 2010-09-07 16:16:30