代码之家  ›  专栏  ›  技术社区  ›  skip

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

  •  2
  • skip  · 技术社区  · 14 年前

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

    非常感谢

    4 回复  |  直到 14 年前
        1
  •  3
  •   skip    14 年前

    以下是需要添加到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行。

    非常感谢

        2
  •  1
  •   skaffman    14 年前

    DispatcherServlet

        3
  •  1
  •   earldouglas    14 年前

    是的,你可以。你需要包括 Spring JavaConfig

    Here is an example 我写了一段时间比较谷歌Guice和Spring。在底部(查找@ImportXml),我展示了如何将springxml配置与注释配置结合起来。配置如下所示:

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

    看到了吗 Spring Reference regarding combining XML and Annotation configuration . 这是Spring3的文档中的内容,但它仍然应该适用(可能对旧的SpringJavaConfig项目中的类名和路径做了一些小的更改)。

        4
  •  0
  •   walkeros    10 年前

    在春季>=3.0使用 @ImportResource

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