流水无情丶 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条)

报告相同问题?

悬赏问题

  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换