我在写Spring整合JDBC框架的时候用了properties文件去设置JDBC的参数。但是发现了一个问题先付上代码

properties文件的代码

db.driverClass=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/exercise
db.username=root
db.password=123456
db.initialSize=100
db.maxTotal=95
db.maxIdle=60
db.minIdle=55

JDBC配置类的代码

package cn.devil.configs;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; @Configuration
@PropertySource(encoding="UTF-8", value = { "classpath:databaseinfo.properties" })
public class SpringJdbcConfig { @Value("${db.driverClass }")
private String driverClass; @Value("${db.url }")
private String url; @Value("${db.username }")
private String username; @Value("${db.password }")
private String password; @Value("${db.initialSize }")
private Integer initialSize; @Value("${db.maxTotal }")
private Integer maxTotal; @Value("${db.maxIdle }")
private Integer maxIdle; @Value("${db.minIdle }")
private Integer minIdle; @Bean("dataSource")
public BasicDataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setInitialSize(initialSize);
dataSource.setMaxTotal(maxTotal);
dataSource.setMaxIdle(maxIdle);
dataSource.setMinIdle(minIdle);
return dataSource;
} @Bean("jdbcTemplate")
public JdbcTemplate getJdbcTemplate(@Autowired BasicDataSource dataSource) {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
} @Bean("transactionManager")
public DataSourceTransactionManager getDataSourceTransactionManager(@Autowired BasicDataSource dataSource){
DataSourceTransactionManager tx = new DataSourceTransactionManager();
tx.setDataSource(dataSource);
return tx;
}
}

这里注意一下Value注解的参数,我为了美观在每个参数后面都加了个空格。结果...

信息: Found 3 annotated classes in package [cn.devil.configs]
四月 22, 2019 6:29:57 下午 org.springframework.web.context.support.AnnotationConfigWebApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springJdbcConfig': Unsatisfied dependency expressed through field 'initialSize'; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "${db.initialSize}"
四月 22, 2019 6:29:57 下午 org.springframework.web.servlet.DispatcherServlet initServletBean
严重: Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springJdbcConfig': Unsatisfied dependency expressed through field 'initialSize'; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "${db.initialSize}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)

他就给我报了一个250*2 !的异常!当时我百思不得其解。明明我写的是个Integer类型的参数去接收的啊,但是为什么它给我返回的却是一个Sting呢?而且为什么其他的都可以就偏偏是int类型的值不行呢?如果Driver也出错的话当时问题就解决了。经过几十次尝试之后还是不行。最后一位dalao跟我说尝试去掉空格。刷一下问题就解决了。但是到底又是为什么会导致这种怪象发生呢?

经过一番尝试之后终于找到了答案。原来OGNL表达式在字符串里面解析的时候是会把后面的空格也解析进去的。例如我输入的"${db.initialSize  }"这个,程序看到的情况是“100  ”。100+空格!这样怎么可能被parseInt呢?自然而然就给我解析成一个String类型的数据了。但是为什么其他参数又没有受到影响呢?其他参数本身就是字符串嘛。再加上那些参数再取值的时候做了去空格的处理自然而然就没有受到影响了。以后对于这种借助表达式传参的情况真得多留个心。

OGNL表达式的一个坑!的更多相关文章

  1. Struts2的标签库(二)——OGNL表达式

    Struts2的标签库(二) --OGNL表达式 1.Struts2中的OGNL表达式增加了ValueStack的支持. 注:ValueStack--实际上是一个容器对象,该对象在启动Struts2框 ...

  2. Ognl表达式基本原理和使用方法

    Ognl表达式基本原理和使用方法 1.Ognl表达式语言 1.1.概述 OGNL表达式 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,他是一个 ...

  3. Struts2的OGNL表达式语言

    一.OGNL的概念 OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表达式语言,它通过简单一致的语法,可以任意存取对象的属性或者 ...

  4. struts2学习笔记--OGNL表达式1

    struts2标签库主要使用的是OGNL语言,类似于El表达式,但是强大得多,它是一种操作对象属性的表达式语言,OGNL有自己的优点: 能够访问对象的方法,如list.size(); 能够访问静态属性 ...

  5. struts2 OGNL表达式

    一.OGNL OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表达式语言,它通过简单一致的语法,可以任意存取对象的属性或者调用对 ...

  6. OGNL表达式

    一.什么是OGNL,有什么特点?     OGNL(Object-Graph Navigation Language),大概可以理解为:对象图形化导航语言.是一种可以方便地操作对象属性的开源表达式语言 ...

  7. OGNL表达式struts2标签“%,#,$”

    一.什么是OGNL,有什么特点? OGNL(Object-Graph Navigation Language),大概可以理解为:对象图形化导航语言.是一种可以方便地操作对象属性的开源表达式语言.OGN ...

  8. JSTL标签,EL表达式,OGNL表达式,struts2标签 汇总

    一下纯属个人总结摘抄,总结一起方便查看,解决疑问,有遗漏或错误,还请指出.       1,JSTL标签总结: a).JSTL标签有什么用?          JSTL是由JCP(Java Commu ...

  9. struts2视频学习笔记 28(OGNL表达式)

    课时28 OGNL表达式 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目. Struts 2框架使用OGNL作为默认的表达式语 ...

随机推荐

  1. C(n,m)排列组合算法

    主要解决C(n,m)问题 static class Extension { public static IList<IList<T>> GetGroup<T>(th ...

  2. Docker生态会重蹈Hadoop的覆辙吗?

    Docker生态会重蹈Hadoop的覆辙吗? http://mp.weixin.qq.com/s?__biz=MzA5NDg3ODMxNw==&mid=2649535024&idx=1 ...

  3. Postman中x-www-form-urlencoded请求K-V的ajax实现

    在Postman中使用x-www-form-urlencoded,并且用K-V传值,但是在代码中用ajax来请求,传值一直有问题,静下心来思考才发现K-V传入的是string,所以记录下来以防忘记!! ...

  4. php中把美国时间转为北京时间的自定义

    我的服务器北京时间,php调用的时间: date.timezone ="America/Chicago" 这是美国这边的一个时间,有的时候跟北京相差13个小时,有的时候跟北京时间相 ...

  5. 查看当前Jquery版本

    <script type="text/javascript"> $(document).ready(function(){ alert(jQuery.fn.jquery ...

  6. 【lintcode】 二分法总结 I

     二分法:通过O(1)的时间,把规模为n的问题变为n/2.T(n) = T(n/2) + O(1) = O(logn). 基本操作:把长度为n的数组,分成前区间和后区间.设置start和end下标.i ...

  7. CentOS 7上安装PGI 2017编译器

    1. 安装PGI编译器 在PGI的官方网站的右上角,有一个社区免费版(Community Edition)的下载链接(GET PGI FOR FREE),根据操作系统选择合适的版本即可. 需要注意的是 ...

  8. 将一幅图像取平均值缩小N倍实现方法

    /// <summary> /// 将图像缩小N倍 /// </summary> /// <param name="source">原图数据&l ...

  9. NullPointerException空指针异常——没有事先加载布局文件到acitivy——缺少:setContentView(R.layout.activity_setup_over);

    空指针异常: 04-27 01:13:57.270: E/AndroidRuntime(4942): FATAL EXCEPTION: main04-27 01:13:57.270: E/Androi ...

  10. Rhino模型制作——京东狗(练习网格切割)

    我最近做了一个京东狗的模型,我先把渲染好的模型给大家看一下. 别看这个模型很复杂,其实京东狗的模型是网上找的,我只是做了一个上面的洞.不过我告诉大家Rhino的下载地址:http://www.xuex ...