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作为默认的表达式语 ...
随机推荐
- Mysql报错[Warning] TIMESTAMP with implicit DEFAULT value is deprecated和Buffered warning: Changed limits
报错2019-04-24 12:06:46 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use -- ...
- win2012服务器配置ftp
1.打开服务器管理器,选择添加角色和功能 2.按照下述向导,安装IIS 选择iis 勾选FTP服务器 3.添加新用户 用户密码按照下图勾选,创建用户 4.IIS配置FTP站点 指定用户访问,并授权,点 ...
- MPI编程——分块矩阵乘法(cannon算法)
https://blog.csdn.net/a429367172/article/details/88933877
- Spring的事务初见
一.事务的特性 原子性: 事务是最小的执行单位,不允许分割.事务的原子性确保动作要么全部完成,要么完全不起作用: 一致性: 执行事务前后,数据保持一致: 隔离性: 并发访问数据库时,一个用户的事物不被 ...
- 【期望dp】绵羊跳弹簧
[期望dp] 绵羊跳弹簧 >>>>题目 [题目] T 组数据.对于每一组数据,有n+1 个格子从0 到n 标号,绵羊从0 号结点开始,每次若在 x 位置掷骰子,令掷出的数为nu ...
- JSP页面传值出现中文乱码的问题
在接收值的jsp页面代码的body里添加: <%request.setCharacterEncoding("utf-8"); %> //这里是设置utf-8为jsp页 ...
- MVP技术沙龙上海站-SQL BI
5月,微软爱好者们齐聚一起,在上海港汇中心,参加<MVP技术沙龙上海站-SQL BI>系列讲座,下面是现场图片.
- springmvc核心技术
目录 异常处理 类型转换器 数据验证 文件上传与下载 拦截器 异常处理 Spring MVC中, 系统的DAO, Service, Controller层出现异常, 均通过throw Exceptio ...
- mvc控制器
控制器 using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.We ...
- 【转载】C++对象成员与构造函数
一个类的对象可以作为另一个类的数据成员,此时把该对象称为类的对象成员. 当一个类中出现对象成员时,该类的构造函数就要为对象成员初始化,对象成员的初始化必须在构造函数的初始化表中完成. 注意: 初始化对 ...