在spring 2.5中可以使用基于注释和基于xml的配置吗?

在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应该在同一个应用程序中同时识别旧式和新式控制器。

+0

非常感谢skaffman :)。你能指导我参考一下,我可以找到如何配置我的应用程序,所以我可以将它们两个一起使用? – skip 2010-09-07 15:58:55

+0

@skip:你不需要,它只是工作。 – skaffman 2010-09-07 16:16:30

是的,你可以。您需要包含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项目的类名和路径进行较小的更改)。

+1

我想你可能误解了这个问题。他指的是旧式控制器和新式注释控制器,而不是“@ Bean”式配置。无可否认,该主题确实另有建议。 – skaffman 2010-09-07 16:17:26

+0

你是对的,我误解了这个问题。我会在这里留下我的答案,以防其他人碰巧发现它有用。 – earldouglas 2010-09-07 16:19:42

在春天> = 3.0使用@ImportResource注释

@Configuration 
@ImportResource({ "classpath:/path/to/spring.xml", }) 
public class AppConfig { 
}