流水无情丶 2017-03-28 14:02 采纳率: 66.7%
浏览 1566
已采纳

Spring4 JDBC 通过JNDI获取数据源

为什么 jndi的id属性一定要是dataSource,如果不是dataSource就报错

 <jee:jndi-lookup id="dataSource" jndi-name="jdbc/ORCL" resource-ref="true"/>

  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
  </bean>
如果将JNDI的id属性设置为datasource,将会报错:
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'helloWorld': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.it.dao.Seacher com.it.controller.HelloWorld.seacher; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'seacher' defined in class path resource [springmvc.xml]: Cannot resolve reference to bean 'jdbcTemplate' while setting bean property 'jdbcTemplate'; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTemplate' defined in class path resource [springmvc.xml]: Error setting property values;
nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'datasource' of bean class [org.springframework.jdbc.core.JdbcTemplate]: Bean property 'datasource' is not writable or has an invalid setter method. Did you mean 'dataSource'?
  • 写回答

2条回答 默认 最新

  • Leon0720 2017-03-29 00:52
    关注

    这里的name一定是dataSource,ref只要和自己定义的id一样即可

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程