-4
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
 log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class    [org.springframework.orm.hibernate4.HibernateTransactionManager] for bean with name 'transactionManager' defined in    class path resource [spring_hibernate.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.HibernateTransactionManager
at   org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1275)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:575)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1344)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:910)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.App.main(App.java:14)
 Caused by: java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.HibernateTransactionManager
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:260)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:416)
at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1296)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1267)
... 9 more

班级

package com;

  import org.springframework.context.ApplicationContext;
     import org.springframework.context.support.ClassPathXmlApplicationContext;

        import com.entity.Ws_security;
     import com.services.Interface_ws_security_services;

    public class App {

private static ApplicationContext ctx1;

public static void main(String[] args) {
    ctx1 = new ClassPathXmlApplicationContext("spring_hibernate.xml");

    Interface_ws_security_services service=(Interface_ws_security_services) ctx1.getBean("a");

    Ws_security ess=new Ws_security();
    ess.setIDws("ess1");
    ess.setLogin("ess2");
    service.addws(ess);
    System.out.println("Done");


      }

        }

Spring Hibernate 的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.2.xsd">



       <context:component-scan base-package="java.com" />
       <context:annotation-config />

       <tx:annotation-driven transaction-manager="transactionManager"/>
       <bean id="transactionManager"  
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory" />  
    </bean> 

    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost/webservices" />
    <property name="username" value="postgres" />
    <property name="password" value="1234" />
  </bean>



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

我正在使用 Maven、Spring 和 Hibernate。我不明白这个问题。我尝试将一些数据插入我的数据库,但我的项目不起作用。

4

2 回答 2

3

异常跟踪中清楚地说明了这一点: Caused by: java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.HibernateTransactionManager

所以你的类路径中缺少spring ormjar 文件,将它添加到你的类路径中并重新测试它。

如果您使用的是 maven,则将此依赖项添加到您的 pom 文件中:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>3.1.3.RELEASE</version>
</dependency>

正确的弹簧<version>标签值取决于您的弹簧版本。

于 2013-07-25T01:24:39.823 回答
0

您需要将 spring orm jar 文件添加到您的类路径中。为此,请将 jar 文件添加到工作区。接下来右键单击您的项目并选择属性。然后转到侧面选项卡“Java 构建路径”,然后转到顶部选项卡“库”,然后按“添加 jars”并导航到 jar 文件的位置。

于 2013-07-25T01:32:02.823 回答