OGNL表达式的一个坑!
我在写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表达式的一个坑!的更多相关文章
- Struts2的标签库(二)——OGNL表达式
Struts2的标签库(二) --OGNL表达式 1.Struts2中的OGNL表达式增加了ValueStack的支持. 注:ValueStack--实际上是一个容器对象,该对象在启动Struts2框 ...
- Ognl表达式基本原理和使用方法
Ognl表达式基本原理和使用方法 1.Ognl表达式语言 1.1.概述 OGNL表达式 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,他是一个 ...
- Struts2的OGNL表达式语言
一.OGNL的概念 OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表达式语言,它通过简单一致的语法,可以任意存取对象的属性或者 ...
- struts2学习笔记--OGNL表达式1
struts2标签库主要使用的是OGNL语言,类似于El表达式,但是强大得多,它是一种操作对象属性的表达式语言,OGNL有自己的优点: 能够访问对象的方法,如list.size(); 能够访问静态属性 ...
- struts2 OGNL表达式
一.OGNL OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表达式语言,它通过简单一致的语法,可以任意存取对象的属性或者调用对 ...
- OGNL表达式
一.什么是OGNL,有什么特点? OGNL(Object-Graph Navigation Language),大概可以理解为:对象图形化导航语言.是一种可以方便地操作对象属性的开源表达式语言 ...
- OGNL表达式struts2标签“%,#,$”
一.什么是OGNL,有什么特点? OGNL(Object-Graph Navigation Language),大概可以理解为:对象图形化导航语言.是一种可以方便地操作对象属性的开源表达式语言.OGN ...
- JSTL标签,EL表达式,OGNL表达式,struts2标签 汇总
一下纯属个人总结摘抄,总结一起方便查看,解决疑问,有遗漏或错误,还请指出. 1,JSTL标签总结: a).JSTL标签有什么用? JSTL是由JCP(Java Commu ...
- struts2视频学习笔记 28(OGNL表达式)
课时28 OGNL表达式 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目. Struts 2框架使用OGNL作为默认的表达式语 ...
随机推荐
- java 形式参数和实际参数的区别
1.形参不能离开方法.形参只有在方法内才会发生作用,也只有在方法中使用,不会在方法外可见.而实参可以再程序的任何地方都使用.
- 记MySQL的一次查询经历
今天在MySQL查数据,sql语句如下: SELECT * FROM `admins` where dep_ids = 24;
- WPF程序打包发布
1.新建安装项目: 新建项目——其他项目类型——安装与部署——InstallShield Limited Edition Project 2.配置安装信息: 选择Project Assistant进入 ...
- 基于Qt的图像处理技术和算法
https://blog.csdn.net/silangquan/article/details/41008183
- printf()、sprintf()、vprintf()、vsprintf()(转)
转自http://sumsung753.blog.163.com/blog/static/14636450120112151092934/ 一.printf() printf()函数优点在于可以格式化 ...
- css属性应用bug大杂烩(后续继续更新)
一.Flex布局使用时的坑: 1.常见的左右分布的flex布局中,左侧给定宽度,右侧占满剩余空间,但当右侧中文字内容很多时,会挤占左侧空间,时左侧不能按照定宽显示. <style> .fa ...
- Daily record-November
November 11. I managed to grab her hand. 我抓到了她的手.2. He passed a hand wearily over his eyes. 他疲倦地用手抹了 ...
- padding属性很有用
html代码中的text-align有时失效,特别是用bootstrap时,用padding-left:xx%,能够很好定位,而且只能够电脑和手机浏览器显示的统一.
- C# DotNetBar ribboncontrol子窗体的系统控件(最大最小关闭)在父窗体中多余显示
初始效果: 重复的子窗体按钮,看着别扭,修改MdiSystemItemVisible = False;后效果: MdiSystemItemVisible的系统解释为: 为了解决这个问题,浪费了好几天. ...
- VsCode编写博客发布
发布图片测试: Java代码测试: //计算机等级考试p6例1.2 //编辑者:鸿灬嗳 package test00; class Circle{ static double PI=3.1415926 ...