@Value("${enable-upload-image}")private String enable;

如上所示,同样的代码,写在在业务层,运行时能取到正确的值,但在控制层却取得了@Value注解中的Key(@Value注解有个特点,如果取不到值,那么不是返回一个null,而是返回Key)。

原因是controller注册在dispatcherservlet-servlet.xml代表的Spring MVC的容器中,而service则注册在application-context.xml代表的Spring的容器中。

如果context:property-placeholder只注册在Spring的容器中,那么自然只有业务层的类可以取到enable-upload-image的值,而控制器取不到值。

解决方法就是把各种context:property-placeholder在两个容器中都注册一下。如:

spring-properties.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:property-placeholderlocation="classpath:wechat/official-account.properties"ignore-unresolvable="true" /><context:property-placeholder location="classpath:conf.properties"ignore-unresolvable="true" /><context:property-placeholder location="classpath:copywriting.properties"ignore-unresolvable="true" /></beans>

application-context.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><import resource="classpath:springframework/spring-*.xml" /></beans>

dispatcherservlet-servlet.xml

<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"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 其他配置忽略 --><import resource="classpath:springframework/spring-properties.xml" /></beans>

转载于:https://www.cnblogs.com/deolin/p/7754047.html

控制层@Value注解取不到值相关推荐

  1. @value注解取不到值_教学笔记:Java注解及自定义注解示例

    现代的Java编程过程中,会经常需要使用到注解,各种流行框架,比如在使用spring进行应用构建的过程中会使用到非常多的spring注解. 本文简要谈一谈Java注解以及如何去定义自己的注解在程序中进 ...

  2. 通过拦截器获取控制类requestMapping注解中的属性值

     1.控制类: /*** 根据id删除*/@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE,n ...

  3. SpringBoot 项目@Value 注解取不到值

    @Value 注解​可能有两种导包: import org.springframework.beans.factory.annotation.Value; 或 import com.google.ap ...

  4. 动手开发自己的mvc-2----完善控制层,提供自动注入和注解上传等功能

    当表单提交的内容过多 ,让懒惰的程序员一个个getParameter()是很让人抓狂的,所以自动注入表单域是mvc不可或缺的功能,另外,文件上传也是一个特殊的表单域,你想看到程序员发觉上传只需要注入就 ...

  5. SpringMVC学习:控制层(Controller)基于注解详解

    文章目录 一.URL映射Controller的方法返回值 二.SpringMVC各类注解详解 (一) @Controller (二) @RequestMapping 1.基本用法 2. path属性或 ...

  6. 思路:controller层:后台如何取值 前端如何给name赋值 例如是id赋值还是自己随意定义...

    思路:controller层:后台如何取值 前端如何给name赋值 例如是id赋值还是自己随意定义 转载于:https://www.cnblogs.com/classmethond/p/1063990 ...

  7. 不拦截指定路径_控制层访问拦截

    在控制层进行访问拦截也是我们在项目中常会遇到的需求,例如:项目中要求系统登录操作有时间限制--12306购票时间的限制等. 对于这类需求我们一般有几种选择: 过滤器Filter AOP SpringM ...

  8. 怎么通过controller层退出登录_控制层访问拦截

    在控制层进行访问拦截也是我们在项目中常会遇到的需求,例如:项目中要求系统登录操作有时间限制--12306购票时间的限制等. 对于这类需求我们一般有几种选择: 过滤器Filter AOP SpringM ...

  9. request.getParameter和SpringMVC后台控制层获取参数的方式

    **1. request.getParameter** 转载于:http://blog.csdn.net/moreorless/article/details/4199677 一. 简单的对比 req ...

最新文章

  1. 华为服务器面板显示,服务器面板怎么查看
  2. 利用位运算和指针实现的交换两个数的程序
  3. HarmonyOS之公共事件的发布、订阅与退订
  4. when is this.oModel in sap-ui-core.js initialized for navigation working
  5. python集合类型变量_Python小白学习之路(八)—【变量】【基本数据类型分类】【集合】【集合的功能】...
  6. linux gcc 7.3.0安装,升级gcc到7.3.0
  7. linux netcat测试udp端口,使用nc(netcat)测试udp协议与端口连通性
  8. centos安装Gitlab Runner
  9. ArcGISEngine二次开发(2):地图制图
  10. PHP实现微博的同步发送(转)
  11. Atitit 机器学习算法分类 目录 1. 传统的机器学习算法 vs 深度学习 1 1.1. 传统的机器学习算法包括决策树、聚类、贝叶斯分类、支持向量机、EM、Adaboost等等。 2 2. 监
  12. ubuntu处理openproj不能使用问题
  13. Shiro完整教程, 附带各种配置
  14. 【Segmentation】
  15. 分析Padavan的代码二
  16. SamSung WLAN AP:RCE漏洞复现
  17. run fsck manually
  18. velocity源码分析:初始化之日志系统
  19. 生存资料决策曲线分析DCA
  20. 入行产品经理所需的准备

热门文章

  1. 计算机考研哈理工好吗,哈尔滨理工大学考研难吗?一般要什么水平才可以进入?...
  2. 刷新存储器的容量单位是什么_SRAM的容量扩展
  3. 南信大滨江学院计算机考试姜青山,【数据库原理】滨江学院姜青山 期末试卷知识点笔记整理 南京信息工程大学...
  4. mysql缺失值处理方法,插值MySQL表中的缺失值
  5. flink的watermark简单理解
  6. VB得到指定文件夹下的文件列表
  7. RoadStar创始人曝光真相:投资方忽然冻结账户,威逼利诱抢公章
  8. 干货警告!国外有个小姐姐给29篇经典机器学习论文写了总结 | 资源
  9. LeCun:深度学习在信号理解中的强大和局限(视频+PPT)
  10. AI说,它可以把你变成个游戏 | 3D人体模型 · CVPR