创建名称为' entityManagerFactory'的bean时出错在ServletContext资源中定义[/WEB-INF/dispatcher-servlet.xml]:

时间:2017-07-11 06:15:20

标签: java spring hibernate spring-mvc servlets

我正在尝试在Apache tomcat和My sql上运行Web应用程序。之前它是在oracle 11g和wildfly上运行的,它运行正常,请找到下面附带的xml配置文件。提前谢谢。

这是我的调度程序-servlet.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:context="http://www.springframework.org/schema/context"
		
		xmlns:mvc="http://www.springframework.org/schema/mvc"
		xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd 
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
  
       <bean id="myBeansValidator"
	       class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/> 
       <mvc:annotation-driven validator="myBeansValidator"/> 
  
  
       <context:component-scan base-package="com.spring.ars"/>
  
       <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		     <!--  <property name="viewClass">
			          <value>org.springframework.web.servlet.view.JstlView</value>
		       </property> --> 
		       <property name="prefix">
		              <value>/</value>
		       </property>
       </bean> 
  
  	<!-- <context:annotation-config />
	<context:component-scan base-package="com.cg"/> -->
	
	 <!-- This bean is required to load ORM persistence and related annotations -->
		<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
	 
	<!-- This bean will hold references to our database -->
	   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
					<property name="driverClassName" value="com.mysql.jdbc.Driver" />
					<property name="url" value="jdbc:mysql://localhost:8080" />
					<property name="username" value="root" />
					<property name="password" value="root" />
	    </bean> 
	
	<!-- Spring way of entity manager factory configuration -->
	
		 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
          	 <property name="dataSource" ref="dataSource"/>
         	 <property name="packagesToScan" value="com.spring.ars"/>
          	 <property name="persistenceProviderClass" value="org.hibernate.jpa.HibernatePersistenceProvider"/>
          	 <property name="jpaPropertyMap">
              	<map>
                  <entry key="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
                   <entry key="hibernate.hbm2ddl.auto" value="update"/> 
                   <entry key="hibernate.show_sql" value="true"/> 
              </map>
             </property>
        </bean> 
      
	<!-- This bean will handle all transaction within an application -->
		<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
			<property name="entityManagerFactory" ref="entityManagerFactory"/>
		</bean>
	
	<!-- This is required to handle transactions via Annotations like @Transactional -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	  
</beans>
&#13;
&#13;
&#13;

这是我的web.xml

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>AirlineReservationSystem</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
   <servlet>
   	 	<servlet-name>dispatcher</servlet-name>
    	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   </servlet>
   
   <servlet-mapping>
    		<servlet-name>dispatcher</servlet-name>
    		<url-pattern>*.do</url-pattern>
   </servlet-mapping>
    
   <jsp-config>
     <jsp-property-group>
       <url-pattern>*.jsp</url-pattern>
     </jsp-property-group>
   </jsp-config>
    
   <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
   </context-param>
	
    <listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  
</web-app>
&#13;
&#13;
&#13;

堆栈跟踪

  

SEVERE:将上下文初始化事件发送到类org.springframework.web.context.ContextLoaderListener的侦听器实例的异常   org.springframework.beans.factory.BeanCreationException:使用名称&#39; entityManagerFactory&#39;创建bean时出错。在ServletContext资源[/WEB-INF/dispatcher-servlet.xml]中定义:无法解析对bean的引用&#39; dataSource&#39;设置bean属性&#39; dataSource&#39 ;;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为&#39; dataSource&#39;的bean时出错。在ServletContext资源[/WEB-INF/dispatcher-servlet.xml]中定义:设置属性值时出错;嵌套异常是org.springframework.beans.PropertyBatchUpdateException;嵌套的PropertyAccessExceptions(1)是:   PropertyAccessException 1:org.springframework.beans.MethodInvocationException:Property&#39; driverClassName&#39;抛出异常;嵌套异常是java.lang.IllegalStateException:无法加载JDBC驱动程序类[com.mysql.jdbc.Driver]       在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)       在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1477)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)       at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:303)       在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)       在org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)       在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)       在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)       at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)       在org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)       在org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)       at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853)       at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)       在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)       在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1408)       在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1398)       at java.util.concurrent.FutureTask.run(Unknown Source)       at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)       在java.lang.Thread.run(未知来源)   引起:org.springframework.beans.factory.BeanCreationException:创建名称为&#39; dataSource&#39;的bean时出错在ServletContext资源[/WEB-INF/dispatcher-servlet.xml]中定义:设置属性值时出错;嵌套异常是org.springframework.beans.PropertyBatchUpdateException;嵌套的PropertyAccessExceptions(1)是:   PropertyAccessException 1:org.springframework.beans.MethodInvocationException:Property&#39; driverClassName&#39;抛出异常;嵌套异常是java.lang.IllegalStateException:无法加载JDBC驱动程序类[com.mysql.jdbc.Driver]       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1514)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1222)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)       at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:303)       在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)       在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)       ......还有24个   引起:org.springframework.beans.PropertyBatchUpdateException;嵌套的PropertyAccessExceptions(1)是:   PropertyAccessException 1:org.springframework.beans.MethodInvocationException:Property&#39; driverClassName&#39;抛出异常;嵌套异常是java.lang.IllegalStateException:无法加载JDBC驱动程序类[com.mysql.jdbc.Driver]       在org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:121)       在org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1510)       ......还有32个

2 个答案:

答案 0 :(得分:1)

答案

如果您使用的是mysql 改变你的hibernate属性

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.spring.ars"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="show_sql">true</prop>
            </props>
        </property>
    </bean>

答案 1 :(得分:0)

错误似乎表明您的类路径中没有MySQL驱动程序:

无法加载JDBC驱动程序类[com.mysql.jdbc.Driver]

请确保在你的应用程序WEB-INF / lib文件夹或tomcat lib文件夹中有正确的MySQL连接器/驱动程序:https://dev.mysql.com/downloads/connector/j/5.0.html

如果您没有手动添加它,或者如果您在项目中使用maven,则可以将其添加到您的pom.xml中:

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.6</version>
</dependency>