当前位置: 首页 > 知识库问答 >
问题:

使用在 ServletContext 资源 [/WEB-INF/调度程序-servlet.xml] 中定义的名称“实体管理器工厂”创建 Bean 时出错:

汪安宁
2023-03-14

我正在尝试在阿帕奇雄猫和我的sql上运行Web应用程序。早些时候它在预言机11g和野蝇上运行并且工作正常,请在下面找到附加的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>

这是我的web.xml

<?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>

堆栈跟踪

共有2个答案

周瀚
2023-03-14

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

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

请确保在您的apps 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>
濮升
2023-03-14

如果您使用的是 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>
 类似资料: