基于Eclipse的Spring4.3.3配置获取DataSource详解

来源:互联网 发布:导播软件 编辑:程序博客网 时间:2024/06/08 08:58

笔者之前用的是MyEclipse 2016 CI 1,MyEclipse内配的Spring版本是4.1.1,以下是Spring4.1.1版本的DataSource配置代码:

<bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property><property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property><property name="username" value="scott"></property><property name="password" value="tiger"></property></bean>


现在用的是Eclipse 4.6Neon,Spring4.3.3,把以上4.1.1版本的配置代码拷贝到现在的程序中执行,出现以下异常:

Error creating bean with name 'dataSource' defined in class path resource [beans.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'driverClass' of bean class [org.springframework.jdbc.datasource.DriverManagerDataSource]: Bean property 'driverClass' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

配置文件运行到<property name="aaa" value="bbb" />时会通过反射获取setAaa方法,bbb作为形参值注入,

而根据异常得知DriverManagerDataSource中没有aaa属性,找到DriverManagerDataSource.class打开查看

若没有添加源码打开后点击Attach Source找到Spring所在目录添加对应的spring-xxx-x.x.x.RELEASE-sources.jar即可

可以看到DriverManagerDataSource没有定义任何属性,CTRL+左击 点击父类AbstractDriverBaseDataSource查看


可以看见包含属性url、username、password、catalog、schema、connectionProperties且都已有对应的setter和getter方法,即这几个属性可通过Spring配置bean property注入,而这版本中driverClassName已被移除,固无需设置,以下乃配置图及测试图(连接池的连接数配置是一些书上的案例,但在DriverManagerDataSource及其父类中都没有这些属性,所以设置这些属性将会出现与设置driverManagerClassName相同的异常——属性不存在):





0 0
原创粉丝点击