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作为默认的表达式语 ...
随机推荐
- vue作用域插槽的应用
问题场景: 存在一个列表,然后当鼠标放入列表中的名称上的时候,自动弹出简介,类似这种效果, 我们当然可以使用positon relative和absolute搭配达到这样的效果,但是现在有一个奇葩的问 ...
- C语言博客作业04——数组
1.本章学习总结 1.1思维导图 1.2本章学习体会及代码量 1.2.1学习体会 这几周学习了数组,数组分为三大块:一维数组.二维数组和字符数组.数组相对于之前普通变量的好处就是可以储存数值,方便数据 ...
- 强化学习(八):Eligibility Trace
Eligibility Traces Eligibility Traces是强化学习中很基本很重要的一个概念.几乎所有的TD算法可以结合eligibility traces获得更一般化的算法,并且通常 ...
- js的中文在网页中显示为乱码
最近的毕业设计写道局部检查用户命是否为空和是否符合规范时 发现页面回显的中文为乱码 then 找到一个和我遇到同样问题的人呐 他说“最近在写一个商城网页的时候遇到了一个问题,那就是javascrip ...
- python - 初识面向对象
1.初识面向对象 面向过程:一切以事务的发展流程为中心 优点:负责的问题流程化,编写相对简单 缺点:可扩展性差,只能解决一个问题,改造也会很困难,牵一发 ...
- css--样式表的引入方法
html引用css方法如下1.直接在div中使用css样式(内链)2.html中使用style自带式(嵌入)3.使用@import引用外部CSS文件(外部引入)4.使用 link引用外部CSS文件 推 ...
- 【Monkey】Monkey稳定性测试常用命令
Monkey稳定性测试常用命令: 1.adb shell monkey n 2.adb shell monkey -p com.android.calculator2 1000 3.adb shel ...
- import com.sun.org.apache.xerces.internal.impl.dv.util.Base64报错
该类不属于JDK标准库范畴,但在JDK中包含了该类,可以直接使用.但是在eclipse中直接使用却找不到该类. 以下是解决方法步骤: Properties-->JavaBulid Path ...
- APP包打包签名步骤
开发混合app上架应用市场,需要进行应用签名,但是申请签名如果没搞过,会特别麻烦,所以我自自己总结了一下申请的步骤,在此记录一下 1.首先需要下载安装java环境即jdk, 2.配置环境变量 假设JD ...
- webpack实践笔记
1)cross-env 能跨平台地设置及使用环境变量 大多数情况下,在windows平台下使用类似于: NODE_ENV=production的命令行指令会卡住,windows平台与POSIX在使用命 ...