我正在尝试在the spring petclinic sample application 中从 hsql 切换到 MySQL。有人可以告诉我我做错了什么吗?
我按照 petclinic_db_setup_mysql.txt 文件中的说明进行操作,并确认在 mysql 中正确创建了数据库,但是当我尝试从 eclipse 中在 tomcat 7 上运行应用程序时出现以下错误:
org.springframework.beans.factory.BeanDefinitionStoreException:
Invalid bean definition with name 'entityManagerFactory' defined in class path resource
[spring/business-config.xml]:
Could not resolve placeholder 'jpa.showSql' in string value "$jpa.showSql"
这里是来自business-config的相关代码:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:database="$jpa.database" p:showSql="$jpa.showSql"/>
<!-- the 'database' parameter refers to the database dialect being used.
By default, Hibernate will use a 'HSQL' dialect because 'jpa.database' has been set to 'HSQL'
inside file spring/data-access.properties
-->
</property>
<!-- gDickens: BOTH Persistence Unit and Packages to Scan are NOT compatible, persistenceUnit will win -->
<property name="persistenceUnitName" value="petclinic"/>
<property name="packagesToScan" value="org.springframework.samples.petclinic"/>
</bean>
这里是 data-access.properties 的内容:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/petclinic
jdbc.username=root
jdbc.password=some_pwd
# Properties that control the population of schema and data for a new data source
jdbc.initLocation=classpath:db/mysql/initDB.sql
jdbc.dataLocation=classpath:db/mysql/populateDB.sql
# Property that determines which Hibernate dialect to use
# (only applied with "applicationContext-hibernate.xml")
hibernate.dialect=org.hibernate.dialect.MySQLDialect
# Property that determines which database to use with an AbstractJpaVendorAdapter
jpa.database=MYSQL
jpa.showSql = true #Added at Dan's suggestion, but new error occurs now.
编辑:
如上所示,我添加了 jpa.showSql=true,但现在抛出以下新错误消息:
ERROR ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'org.springframework.jdbc.datasource.init.DataSourceInitializer#0':
Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException:
Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException:
Could not get JDBC Connection;
nested exception is java.sql.SQLException: com.mysql.jdbc.Driver
我确认数据库的密码是正确的(不是上面的假密码),并且我能够在 MySQL 命令行客户端中导航数据库。所以问题在于将 Web 应用程序连接到数据库。有没有人有进一步的建议?
应 Sotirios 的要求,这里是同一错误的更完整堆栈跟踪:
Nov 28, 2013 1:51:31 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.jdbc.datasource.init.DataSourceInitializer#0': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: com.mysql.jdbc.Driver
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.springframework.dao.DataAccessResourceFailureException: Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: com.mysql.jdbc.Driver
at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:56)
at org.springframework.jdbc.datasource.init.DataSourceInitializer.afterPropertiesSet(DataSourceInitializer.java:83)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
... 22 more
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: com.mysql.jdbc.Driver
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:45)
... 25 more
Caused by: java.sql.SQLException: com.mysql.jdbc.Driver
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:254)
at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:182)
at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:702)
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:634)
at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:488)
at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:144)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:116)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:103)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:127)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
... 26 more
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:246)
... 36 more
Nov 28, 2013 1:51:31 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
ERROR ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.jdbc.datasource.init.DataSourceInitializer#0': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: com.mysql.jdbc.Driver
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512) ~[spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521) ~[spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) ~[spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296) ~[spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) ~[spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293) ~[spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628) ~[spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) ~[spring-context-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) ~[spring-context-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) ~[spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) ~[spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) [spring-web-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939) [catalina.jar:7.0.42]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) [catalina.jar:7.0.42]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:7.0.42]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) [catalina.jar:7.0.42]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) [catalina.jar:7.0.42]
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) [na:1.6.0_29]
at java.util.concurrent.FutureTask.run(FutureTask.java:138) [na:1.6.0_29]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [na:1.6.0_29]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [na:1.6.0_29]
at java.lang.Thread.run(Thread.java:662) [na:1.6.0_29]
Caused by: org.springframework.dao.DataAccessResourceFailureException: Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: com.mysql.jdbc.Driver
at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:56) ~[spring-jdbc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.jdbc.datasource.init.DataSourceInitializer.afterPropertiesSet(DataSourceInitializer.java:83) ~[spring-jdbc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571) ~[spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509) ~[spring-beans-3.2.5.RELEASE.jar:3.2.5.RELEASE]
... 22 common frames omitted
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: com.mysql.jdbc.Driver
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80) ~[spring-jdbc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:45) ~[spring-jdbc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
... 25 common frames omitted
Caused by: java.sql.SQLException: com.mysql.jdbc.Driver
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:254) ~[tomcat-jdbc-7.0.42.jar:na]
at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:182) ~[tomcat-jdbc-7.0.42.jar:na]
at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:702) ~[tomcat-jdbc-7.0.42.jar:na]
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:634) ~[tomcat-jdbc-7.0.42.jar:na]
at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:488) ~[tomcat-jdbc-7.0.42.jar:na]
at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:144) ~[tomcat-jdbc-7.0.42.jar:na]
at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:116) ~[tomcat-jdbc-7.0.42.jar:na]
at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:103) ~[tomcat-jdbc-7.0.42.jar:na]
at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:127) ~[tomcat-jdbc-7.0.42.jar:na]
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) ~[spring-jdbc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) ~[spring-jdbc-3.2.5.RELEASE.jar:3.2.5.RELEASE]
... 26 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714) ~[catalina.jar:7.0.42]
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) ~[catalina.jar:7.0.42]
at java.lang.Class.forName0(Native Method) ~[na:1.6.0_29]
at java.lang.Class.forName(Class.java:247) ~[na:1.6.0_29]
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:246) ~[tomcat-jdbc-7.0.42.jar:na]
... 36 common frames omitted
Nov 28, 2013 1:51:31 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/petclinic] startup failed due to previous errors
【问题讨论】:
你注释掉data-access.properties中的HSQL设置了吗? 你不需要在 data-access.properties 中添加 jpa.showSql=true @FredericClose 谢谢。我进行了更改,但现在抛出了一个新错误。我在上面的原始帖子的编辑中对其进行了描述。您还有什么建议吗? @Dan 是的,我注释掉了 hsql 设置。以上是我的 data-access.properties 文件中唯一未注释的代码。 这个异常肯定还有更多。可以发一下吗? 【参考方案1】:对于原始错误,see Dan's answer。当 Spring 发现无法解析的属性时,它会立即失败,因此直到修复了这个问题,它才会解决下一个问题。
对于ClassNotFoundException
,答案始终是您的类路径中缺少一个类。在这种情况下,您缺少 com.mysql.jdbc.Driver
,您已将其指定为 JDBC 驱动程序类。
假设您使用的是 maven,您需要为 MySQL 驱动程序添加一个依赖项。如果你没有使用 maven,你需要下载 MySQL 驱动 jar 并将它放在你的 webapp 的WEB-INF/lib
目录中。
【讨论】:
谢谢。 +1。由于您在其他帖子中教给我的东西,我能够如此迅速地找到并在 pom.xml 中进行更改。非常感谢您对本网站的贡献。感恩节快乐。 @CodeMed 仅供参考,我不会忽视你。我看看你的问题。我只是不总是有答案。 @CodeMed 好吧,你在找什么? 过去你告诉我,当我在 spring mvc 应用程序中遇到类未找到异常时要查找什么。你说某些特别的东西总是首先要检查的东西。Can you refresh my memory of what to look for?
我收到一个存在的类的类未找到错误。在我进行了一些不相关的更改后,该应用程序现在不会初始化。如果您知道在这个常见错误中要查找的几件简单的事情,我想避免在新问题中发布大量代码。
@CodeMed 简单检查是(在 Eclipse 中)执行 CTRL + SHIFT + T 并搜索类型。如果没有找到,那么它几乎肯定不在您的工作空间中。【参考方案2】:
将jpa.showSql = true
(或false
,取决于您是否想在控制台中看到)添加到您的data-access.properties
文件中。
【讨论】:
谢谢。 +1 试图提供帮助。但是现在发生了一个新的数据库错误。我在上面的原始帖子的编辑中对其进行了描述。你有什么建议吗? 我在对宠物诊所应用程序进行后续修改时遇到问题。你有一点时间来帮助我吗?这是另一个问题的链接:***.com/questions/20292152/…【参考方案3】:我知道这有点晚了,但我也遇到了 Spring 的“宠物诊所”示例项目的这个问题,并找到了解决方案。
为了使项目能够使用 MySQL,请按照以下步骤操作:
1 - 在 pom.xml 中取消注释第 196 - 200 行(这是为了启用 MySQL 驱动程序)
<!-- For MySql only -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>$mysql-driver.version</version>
</dependency>
2 - 在 data-access.properties 中,将第 8-9 行更改如下:
jdbc.initLocation=classpath:db/mysql/initDB.sql
jdbc.dataLocation=classpath:db/mysql/populateDB.sql
...然后注释第 16 - 22 行
...取消注释第 28 - 34 行(不包括第 33 行)
应该这样做!
【讨论】:
感谢您和 +1 抽出宝贵时间为这个老问题添加见解。仅供参考,您最好学习 spring boot 和 angularjs 而不是旧的 petclinic,因为旧的 petclinic 使用 jsp,这不是现代技术。 petclinic 有一个 angularjs 分支,但它是一个中间示例。初学者可以通过使用 spring boot 教程从头开始构建来了解更多信息,然后只需使用来自 github 的东西添加前端,您可以从谷歌搜索 spring boot angularjs github