@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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder
location="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/beans
http://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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 其他配置忽略 --> <import resource="classpath:springframework/spring-properties.xml" /> </beans>

控制层@Value注解取不到值的更多相关文章

  1. SpringBoot自定义控制层参数解析

    一.背景 在Spring的Controller中,我们通过@RequestParam或@RequestBody就可以将请求中的参数映射到控制层具体的参数中,那么这个是怎么实现的呢?如果我现在控制层中的 ...

  2. 通过反射获取SSM的controller层的注解以及注解中的value值

    package com.reflection.test; import java.lang.annotation.Annotation; import java.lang.reflect.Invoca ...

  3. springMvc基本注解:@Component、@Repository(持久层) 、@Service(业务逻辑) 、@Controller(控制层)

    1.@Controller(控制层) :就是action层 2.@Service(业务逻辑) :业务逻辑层,负责处理各种控制层的操作 3.@Repository(持久层) :称为“持久化”层,负责对数 ...

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

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

  5. spring注解@Value取不到值【转】

    spring注解@Value取不到值 今天在一个项目中发现一个情况,在Service中取不到name值,直接输出了{name}字符串,找了好久,最后在一篇文章中找到解决方案. 解决这个问题的一篇文章( ...

  6. Springboot 使用 JSR 303 对 Controller 控制层校验及 Service 服务层 AOP 校验,使用消息资源文件对消息国际化

    导包和配置 导入 JSR 303 的包.hibernate valid 的包 <dependency> <groupId>org.hibernate.validator< ...

  7. JSF在ui:include中传递参数到对应控制层

    在JSF中使用ui:include方法可以引入一个页面到当前页面中,如果要向被包含的页面中传入参数,可以使用ui:param标签,这个标签类似于f:param,只不过一个用于页面,一个用于实际标签.示 ...

  8. @Value取不到值引出的spring的2种配置文件applicationContext.xml和xxx-servlet.xml

    项目中经常会用到配置文件,定义成properties的形式比较常见,为了方便使用一般在spring配置文件中做如下配置: <context:property-placeholder ignore ...

  9. 再探 Ext JS 6 (sencha touch/ext升级版) 变化篇 (编译命令、滚动条、控制层、模型层、路由)

    从sencha touch 2.4.2升级到ext js 6,cmd版本升级到6.0之后发生了很多变化 首先从cmd说起,cmd 6 中sencha app build package不能使用了,se ...

随机推荐

  1. 在论坛中出现的比较难的sql问题:12(递归问题2 拆分字符串)

    原文:在论坛中出现的比较难的sql问题:12(递归问题2 拆分字符串) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有 ...

  2. Windows中的库编程

    Windows操作系统中,库分为动态链接库(dll)和静态链接库(lib) 动态库是Windows中实现代码共享的一种方式.它是一个二进制式文件,不可单独运行,需要调用方调用才能运行.在Windows ...

  3. django pk 和id用法

    pk就是primary key的缩写,也就是任何model中都有的主键,那么id呢,大部分时候也是model的主键,所以在这个时候我们可以认为pk和id是完全一样的. class Student(mo ...

  4. [ICCV 2019] Weakly Supervised Object Detection With Segmentation Collaboration

    新在ICCV上发的弱监督物体检测文章,偷偷高兴一下,贴出我的poster,最近有点忙,话不多说,欢迎交流- https://arxiv.org/pdf/1904.00551.pdf http://op ...

  5. 笔记一下debian8升级到debian9遇到的几个坑

    由于debian8不再维护了,出于安全需要,参照官方的方法,在线升级到debian9,结果遇到了好多坑,虽然没死人,但也够惨了 坑1.升级后,多占了很多空间,莫明奇妙的把 / 分区占的满满的,由于之前 ...

  6. 【深度学习】基于Pytorch的ResNet实现

    目录 1. ResNet理论 2. pytorch实现 2.1 基础卷积 2.2 模块 2.3 使用ResNet模块进行迁移学习 1. ResNet理论 论文:https://arxiv.org/pd ...

  7. STM32 HAL库学习系列第8篇---回调函数总结

    普通函数与回调函数的区别:就是ST将中断封装,给使用者的API,就是标准库的中断函数 对普通函数的调用: 调用程序发出对普通函数的调用后,程序执行立即转向被调用函数执行,直到被调用函数执行完毕后,再返 ...

  8. clamscan-Linux查毒工具

    转载:https://www.cnblogs.com/tdcqma/p/7576183.html clamscan命令用于扫描文件和目录,一发现其中包含的计算机病毒,clamscan命令除了扫描lin ...

  9. 单链表(python)

    # -*- coding: utf-8 -*- class Node(object): def __init__(self, value=None, next=None): # 这里我们 root 节 ...

  10. 0014SpringBoot结合thymeleaf实现登录功能

    该登录功能需要实现的需求如下: 1.输入用户名密码,如果验证通过,进入首页,并显示登录的用户名 2.如果验证不通过,则重新进入登录页面,并显示“用户名密码错误” 3.如果未经登录,不能直接访问首页等静 ...