使用Hibernate的Eclipse中的Spring程序无法运行

时间:2015-09-23 07:49:06

标签: java eclipse spring hibernate

这是我在执行Spring程序时收到的错误

            Sep 23, 2015 12:11:09 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
            INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3eb07fd3: startup date [Wed Sep 23 12:11:09 IST 2015]; root of context hierarchy
            Sep 23, 2015 12:11:09 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
            INFO: Loading XML bean definitions from class path resource [accounttest.xml]
            Sep 23, 2015 12:11:10 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
            INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
            SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
            SLF4J: Defaulting to no-operation (NOP) logger implementation
            SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
            Sep 23, 2015 12:11:10 PM org.springframework.context.support.ClassPathXmlApplicationContext refresh
            WARNING: Exception encountered during context initialization - cancelling refresh attempt
            org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [accounttest.xml]: Invocation of init method failed; nested exception is org.hibernate.InvalidMappingException: Unable to read XML
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
                at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
                at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
                at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
                at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
                at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736)
                at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
                at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
                at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
                at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
                at com.test.spring.hibernate.HibernateTxLab1.main(HibernateTxLab1.java:12)
            Caused by: org.hibernate.InvalidMappingException: Unable to read XML
                at org.hibernate.util.xml.MappingReader.readMappingDocument(MappingReader.java:101)
                at org.hibernate.cfg.Configuration.add(Configuration.java:513)
                at org.hibernate.cfg.Configuration.add(Configuration.java:509)
                at org.hibernate.cfg.Configuration.add(Configuration.java:716)
                at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:756)
                at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:356)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
                ... 12 more
            Caused by: org.dom4j.DocumentException: Error on line 11 of document  : Element type "property" must be followed by either attribute specifications, ">" or "/>". Nested exception: Element type "property" must be followed by either attribute specifications, ">" or "/>".
                at org.dom4j.io.SAXReader.read(SAXReader.java:482)
                at org.hibernate.util.xml.MappingReader.readMappingDocument(MappingReader.java:75)
                ... 19 more

            Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [accounttest.xml]: Invocation of init method failed; nested exception is org.hibernate.InvalidMappingException: Unable to read XML
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1566)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
                at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
                at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
                at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
                at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
                at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:736)
                at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
                at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
                at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
                at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
                at com.test.spring.hibernate.HibernateTxLab1.main(HibernateTxLab1.java:12)
            Caused by: org.hibernate.InvalidMappingException: Unable to read XML
                at org.hibernate.util.xml.MappingReader.readMappingDocument(MappingReader.java:101)
                at org.hibernate.cfg.Configuration.add(Configuration.java:513)
                at org.hibernate.cfg.Configuration.add(Configuration.java:509)
                at org.hibernate.cfg.Configuration.add(Configuration.java:716)
                at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:756)
                at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:356)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
                ... 12 more
            Caused by: org.dom4j.DocumentException: Error on line 11 of document  : Element type "property" must be followed by either attribute specifications, ">" or "/>". Nested exception: Element type "property" must be followed by either attribute specifications, ">" or "/>".
                at org.dom4j.io.SAXReader.read(SAXReader.java:482)
                at org.hibernate.util.xml.MappingReader.readMappingDocument(MappingReader.java:75)
                ... 19 more

我不确定我的问题是什么,但这就是我所拥有的:

这是我的Account.java

            package com.test.spring.hibernate;


            public class Account {
                private int accno;
                private String aname;
                private double bal;

                public Account() {
                }

                public Account(int accno, String aname, double bal) {
                    this.accno = accno;
                    this.aname = aname;
                    this.bal = bal;
                }

                public int getAccno() {
                    return accno;
                }

                public String getAname() {
                    return aname;
                }

                public double getBal() {
                    return bal;
                }

                public void setAccno(int accno) {
                    this.accno = accno;
                }

                public void setAname(String aname) {
                    this.aname = aname;
                }

                public void setBal(double bal) {
                    this.bal = bal;
                }
            }

这是AccountDAO.java

        package com.test.spring.hibernate;

        public interface AccountDAO {
            public void addAccount(Account a);
            public double deposit(int accno,double amt);
            public double withdraw(int accno,double amt);
            public void transferFunds(int saccno,int daccno,double amt);
        }

这是AccountDAOImpl.java

        package com.test.spring.hibernate;

        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.orm.*;
        import org.springframework.orm.hibernate4.HibernateTemplate;

        import javax.naming.InsufficientResourcesException;

        import java.lang.Exception;
        import java.lang.System;

        public class AccountDAOImpl implements AccountDAO {

            @Autowired
            HibernateTemplate hibernateTemp;

            public void addAccount(Account acc) {
                hibernateTemp.save(acc);
            }

            public double deposit(int accno, double amt) {

                Account acc = (Account) hibernateTemp.load(Account.class, accno);
                double bal = acc.getBal() + amt;
                acc.setBal(bal);
                hibernateTemp.update(acc);

                return bal;
            }

            public double withdraw(int accno, double amt) {
                double bal = 0.0;
                Account acc = (Account) hibernateTemp.load(Account.class, accno);
                if (acc.getBal() - amt >= 5000) {
                    bal = acc.getBal() - amt;
                    acc.setBal(bal);
                    hibernateTemp.update(acc);
                } else {
                    throw new InsufficientFundsException();
                }

                return bal;
            }

            public void transferFunds(int saccno,int daccno,double amt) {
                double dbal = 0.0;
                double sbal = 0.0;

                Account dacc = (Account) hibernateTemp.load(Account.class, daccno);
                System.out.println(dacc.getBal());
                dbal = dacc.getBal() + amt;
                dacc.setBal(dbal);
                hibernateTemp.update(dacc);
                dacc = (Account) hibernateTemp.load(Account.class, daccno);
                System.out.println(dacc.getBal());
                Account sacc = (Account) hibernateTemp.load(Account.class, saccno);
                if (sacc.getBal() - amt >= 5000) {
                    sbal = sacc.getBal() - amt;
                    sacc.setBal(sbal);
                    hibernateTemp.update(sacc);
                } else {
                    throw new InsufficientFundsException();
                }
            }
        }

这是AccountService.java

    package com.test.spring.hibernate;

    public interface AccountService{
        public void addAccount(Account a);
        public double deposit(int accno,double amt);
        public double withdraw(int accno,double amt);
        public void transferFunds(int saccno,int daccno,double amt);
    }

这是AccountServiceImpl.java

    package com.test.spring.hibernate;

    import org.springframework.beans.factory.annotation.Autowired;

    public class AccountServiceImpl implements AccountService{
        @Autowired
        AccountDAO accountDAO=null;
        public void setAccountDAO(AccountDAO accountDAO) {
            this.accountDAO = accountDAO;
        }

        public void addAccount(Account a){
            accountDAO.addAccount(a);
        }

        public double deposit(int accno,double amt){
            return accountDAO.deposit(accno,amt);
        }

        public double withdraw(int accno,double amt){
            return accountDAO.withdraw(accno,amt);
        }

        public void transferFunds(int saccno,int daccno,double amt){
            accountDAO.transferFunds(saccno,daccno,amt);
        }
    }

这是HibernateTxLab1.java

    package com.test.spring.hibernate;

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

    import java.lang.Exception;
    import java.lang.String;
    import java.lang.System;

    public class HibernateTxLab1{
        public static void main(String as1[]){
            ApplicationContext ctx=new ClassPathXmlApplicationContext("accounttest.xml");
            AccountService as=(AccountService)ctx.getBean("accountService");
            try{
            as.transferFunds(101,102,24000);
            }catch(Exception e){
                System.out.println(e);
            }
        }
    }

这是InsufficientFundsException.java

    package com.test.spring.hibernate;

    import java.lang.RuntimeException;
    import java.lang.String;

    public class InsufficientFundsException extends RuntimeException{
        public String toString(){
            return "com.jlcindia.spring.jdbc.InsufficientFundsException:Sorry..Your account has Insufficient Funds";
        }
    }

这是我在项目的Source文件夹中的Hibernate文件Account.hbm.xml

        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

        <hibernate-mapping package="com.test.spring.hibernate">
            <class name="Account" table="accounts" lazy="false">
                <id name="accno" type="int" column="accno">
                    <generator class="increment"/>
                </id>
                <property name="aname" column="aname"/>
                <property name="bal"column="bal" type="double"/>
            </class>
        </hibernate-mapping>

这是我在项目中使用Eclipse

配置的accounttest.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        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"
        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/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">


        <context:annotation-config/>
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
            <property name="url"><value>jdbc:mysql://localhost/test</value></property>
            <property name="username"><value>root</value></property>
            <property name="password"><value>1234</value></property>
        </bean>
        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref ="dataSource"/>
            <property name="mappingResources">
                <list>
                    <value>com/test/spring/hibernate/Account.hbm.xml</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="show_sql">true</prop>
                </props>
            </property>
        </bean>
        <bean id="txManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager" autowire="constructor"/>

        <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"
            autowire="constructor"/>

        <bean id="accountDAO" class="com.test.spring.hibernate.AccountDAOImpl"/>
        <bean id="accountService" class="com.test.spring.hibernate.AccountServiceImpl"/>

        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="transfer*" propagation="REQUIRES_NEW" isolation="READ_COMMITTED"/>
                <tx:method name="*"/>
            </tx:attributes>
        </tx:advice>

        <aop:config>
            <aop:pointcut id="txPointcut" expression="execution(* com.test.spring.hibernate.*Service.*(..))"/>
            <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>      
        </aop:config>
    </beans>

最后我的罐子用了

antlr-2.7.6.jar
cglib-2.2.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate3.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-testing.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
slf4j-api-1.6.1.jar
aopalliance-1.0.jar
commons-logging-1.1.3.jar
spring-aop-4.1.5.RELEASE.jar
spring-aspects-4.1.5.RELEASE.jar
spring-beans-4.1.5.RELEASE.jar
spring-context-4.1.5.RELEASE.jar
pring-context-support-4.1.5.RELEASE.jar
spring-core-4.1.5.RELEASE.jar
spring-expression-4.1.5.RELEASE.jar
spring-instrument-4.1.5.RELEASE.jar
spring-instrument-tomcat-4.1.5.RELEASE.jar
spring-jdbc-4.1.5.RELEASE.jar
spring-jms-4.1.5.RELEASE.jar
spring-messaging-4.1.5.RELEASE.jar
spring-orm-4.1.5.RELEASE.jar
spring-oxm-4.1.5.RELEASE.jar
spring-test-4.1.5.RELEASE.jar
spring-tx-4.1.5.RELEASE.jar
mysql.jar
aspectjrt-1.8.6.jar
aspectjtools-1.6.6.jar
aspectjweaver-1.8.6.jar
hibernate-core-4.1.2.Final.jar

0 个答案:

没有答案