ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring MVC 2.5使用注释和XML配置的混合,但xml被忽略

2019-08-26 23:20:10  阅读:194  来源: 互联网

标签:applicationcontext spring spring-mvc dependency-injection annotations


在我的Spring MVC web应用程序中,我想将基于xml的配置与注释混合:
我使用@Controller,@ RequestMapping(“bla.htm”),@ RequestParam等注释来解析HttpRequests到Controller Methods.所以我补充道

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<context:component-scan base-package="somePackage.controller"/>

到我的dispatcher-servlet.xml.

但我的控制器有属性.这些属性可以通过@AutoWired注释注入.但我也做了定义范围.所以每个属性我会有两个注释,这使得代码可读性差.所以我想在applicationContext.xml文件中注入依赖项.

有没有办法可以保持注释驱动的请求映射,但是使用context.xml文件进行依赖注入?或者是否只能使用EITHER注释或xml配置?

注意:我的依赖注入bean是在不同的xml文件中.

PS:

我应该提到,我使用Spring 2.5并且无法升级它.

解决方法:

不,< mvc:annotation-driven>适用于XML.但是你需要摆脱< context:component-scan>.

更新:使用Spring 2.5,这应该让你开始:

<?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"
    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
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <!-- now add some controllers -->

</beans>

标签:applicationcontext,spring,spring-mvc,dependency-injection,annotations
来源: https://codeday.me/bug/20190826/1734740.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有