Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring2.5中是否可以同时使用基于注释和基于xml的配置?_Spring_Spring Mvc - Fatal编程技术网

Spring2.5中是否可以同时使用基于注释和基于xml的配置?

Spring2.5中是否可以同时使用基于注释和基于xml的配置?,spring,spring-mvc,Spring,Spring Mvc,我一直在做一个项目,在这个项目中,控制器被编写成扩展控制器类。我是否可以在同一应用程序中配置和使用基于POJO的控制器(使用@Controller) 非常感谢。你可以随意把它们混合在一起DispatcherServlet应该在同一个应用程序中同时识别旧式和新式控制器。是的,您可以。您需要包括项目库,因为注释配置不是2.5核心的一部分 不久前,我写了一篇文章,将谷歌Guice与Spring进行比较。在底部(查找@ImportXml),我展示了如何将SpringXML配置与注释配置结合起来。配置如下

我一直在做一个项目,在这个项目中,控制器被编写成扩展控制器类。我是否可以在同一应用程序中配置和使用基于POJO的控制器(使用@Controller)


非常感谢。你可以随意把它们混合在一起
DispatcherServlet
应该在同一个应用程序中同时识别旧式和新式控制器。

是的,您可以。您需要包括项目库,因为注释配置不是2.5核心的一部分

不久前,我写了一篇文章,将谷歌Guice与Spring进行比较。在底部(查找@ImportXml),我展示了如何将SpringXML配置与注释配置结合起来。配置如下所示:

@Configuration
@ImportXml(locations = "classpath:com/earldouglas/guicespringjc/spring/config.xml")
public class XmlSpringConfiguration {
}

看。这是Spring3的文档,但它仍然适用(可能对旧SpringJavaConfig项目中的类名和路径做了一些小改动)。

感谢Jamestatic和skaffman,现在一切正常:)

以下是需要添加到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>
…第3行
…第4行
…第5行
…第6行
…第7行
…第8行
我懒得不在主应用程序中添加第8行

非常感谢

在春天>=3.0使用注释

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

非常感谢斯卡夫曼:)。你能给我一些参考资料吗?在那里我可以找到如何配置我的应用程序,这样我就可以同时使用它们了。我想你可能误解了这个问题。他指的是老式控制器和新式带注释的控制器,而不是
@Bean
-style配置。不可否认,这个问题确实表明了相反的观点。你是对的,我误解了这个问题。我会把我的答案留在这里,以防其他人碰巧发现它有用。