这是我的mysql-ds.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>jdbc/DefaultDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/usuarios</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>da</password>
<valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
这是我在context.xml中的bean
<beans>
<bean name="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>jdbc/DefaultDS</value>
</property>
</bean>
这是正确的方法吗?当我运行我的junit测试时会发生此错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [context.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
这是我在本网站的第一个问题,谢谢。
答案 0 :(得分:0)
我认为你需要&#34; java:jdbc / DefaultDS&#34;
所以bean的定义如下:
<bean name="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:jdbc/DefaultDS</value>
</property>
</bean>