我在写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类型的数据了。但是为什么其他参数又没有受到影响呢?其他参数本身就是字符串嘛。再加上那些参数再取值的时候做了去空格的处理自然而然就没有受到影响了。以后对于这种借助表达式传参的情况真得多留个心。