1

运行后:mvn jetty:run


HTTP 错误:503

访问 /ContactManager 时出现问题。原因:

Service Unavailable

由码头提供支持://

在日志文件中 - 错误:

错误:org.springframework.web.context.ContextLoader - 上下文初始化失败 嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 org.hibernate.SessionFactory org.taranov.contactmanager.dao.ContactDAOImpl.sessionFactory;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/spring/data.xml] 中定义名称为“sessionFactory”的 bean 创建错误:调用 init 方法失败;嵌套异常是 org.hibernate.service.spi.ServiceException:无法创建请求的服务 [org.hibernate.service.jdbc.connections.spi.ConnectionProvider] DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290) at org .codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230) 在 org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409) 在 org.codehaus.plexus.classworlds.launcher .Launcher.main(Launcher.java:352) 原因:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 org.hibernate.SessionFactory org.taranov.contactmanager.dao.ContactDAOImpl.sessionFactory; 嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“sessionFactory”的 bean 时出错 在 ServletContext 资源 [/WEB-INF/spring/data.xml] 中定义:调用 init 方法失败;嵌套异常是 org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.service.jdbc.connections.spi.ConnectionProvider] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject( AutowiredAnnotationBeanPostProcessor.java:506) 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284) .. . 55 更多原因:org.springframework.beans.factory.BeanCreationException: Error created bean with name 'sessionFactory' 在 ServletContext 资源 [/WEB-INF/spring/data.xml] 中定义:调用 init 方法失败;嵌套异常是 org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.service.jdbc.connections.spi.ConnectionProvider] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory. java:1455) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 在 org.springframework. .beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) 在 org.springframework.beans.factory。hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator.instantiateExplicitConnectionProvider(ConnectionProviderInitiator.java:189) ... 88 更多 2012-10-03 23:57:37.640:WARN:oejw.WebAppContext:Failed startup of context omjpJettyWebAppContext{/, file:/C:/jtty/wrk/ContactManager/src/main/webapp/},file:/C:/jtty/wrk/ContactManager/src/main/webapp/org.springframework.beans.factory.BeanCreationException: 创建错误名称为“contactDAOImpl”的 bean:自动装配依赖项的注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 org.hibernate.SessionFactory org.taranov.contactmanager.dao.ContactDAOImpl.sessionFactory;嵌套异常是 org.springframework.beans.factory.BeanCreationException: Error created bean with name ' ServletContext 资源 [/WEB-INF/spring/data.xml] 中定义的 sessionFactory':调用 init 方法失败;嵌套异常是 org.hibernate.service.spi.ServiceException:无法创建请求的服务 [org.hibernate.service.jdbc.connections.spi.ConnectionProvider]

等等..

package org.taranov.contactmanager.dao;

import java.util.List;

import org.taranov.contactmanager.domain.Contact;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class ContactDAOImpl implements ContactDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public void addContact(Contact contact) {
        sessionFactory.getCurrentSession().save(contact);
    }

    @SuppressWarnings("unchecked")
    public List<Contact> listContact() {

        return sessionFactory.getCurrentSession().createQuery("from Contact")
            .list();
    }

    public void removeContact(Integer id) {
        Contact contact = (Contact) sessionFactory.getCurrentSession().load(
                Contact.class, id);
        if (null != contact) {
            sessionFactory.getCurrentSession().delete(contact);
        }

    }
}






    package org.taranov.contactmanager.dao;





    import java.util.List;
    import org.taranov.contactmanager.domain.Contact;

    public interface ContactDAO {

        public void addContact(Contact contact);

        public List<Contact> listContact();

        public void removeContact(Integer id);
    }


web.xml

    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>
    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener>
    <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    <filter>
    <filter-name>charsetFilter</filter-name>
    <filter-class>
    org.springframework.web.filter.CharacterEncodingFilter
    </filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>charsetFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    </web-app>







servlet-context.xml


    > <?xml version="1.0" encoding="UTF-8" ?>  <beans:beans
    > xmlns="http://www.springframework.org/schema/mvc"
    > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    > xmlns:beans="http://www.springframework.org/schema/beans"
    > xsi:schemaLocation="http://www.springframework.org/schema/mvc
    > http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    > http://www.springframework.org/schema/beans
    > http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    > <annotation-driven />  <resources mapping="/resources/**"
    > location="/resources/" />  <beans:bean
    > class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    > <beans:property name="prefix" value="/WEB-INF/views/" /> 
    > <beans:property name="suffix" value=".jsp" />  </beans:bean>
    > <beans:import resource="controllers.xml" />  </beans:beans>








root-context.xml

    > <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:context="http://www.springframework.org/schema/context"
    > xmlns:jee="http://www.springframework.org/schema/jee"
    > xmlns:lang="http://www.springframework.org/schema/lang"
    > xmlns:p="http://www.springframework.org/schema/p"
    > xmlns:tx="http://www.springframework.org/schema/tx"
    > xmlns:util="http://www.springframework.org/schema/util"
    > xsi:schemaLocation="http://www.springframework.org/schema/beans
    > http://www.springframework.org/schema/beans/spring-beans.xsd
    > http://www.springframework.org/schema/aop
    > http://www.springframework.org/schema/aop/spring-aop.xsd
    > http://www.springframework.org/schema/context
    > http://www.springframework.org/schema/context/spring-context.xsd
    > http://www.springframework.org/schema/jee
    > http://www.springframework.org/schema/jee/spring-jee.xsd
    > http://www.springframework.org/schema/lang
    > http://www.springframework.org/schema/lang/spring-lang.xsd
    > http://www.springframework.org/schema/tx
    > http://www.springframework.org/schema/tx/spring-tx.xsd
    > http://www.springframework.org/schema/util
    > http://www.springframework.org/schema/util/spring-util.xsd">
    > <context:annotation-config/> <context:component-scan
    > base-package="org.taranov.contactmanager.dao"/>
    > <context:component-scan
    > base-package="org.taranov.contactmanager.service"/> <import
    > resource="data.xml"/> <import resource="security.xml"/> </beans>

db_server feel fine and in config

    > jdbc.driverClassName= com.mysql.jdbc.Driver
    > jdbc.dialect=org.hibernate.dialect.MySQLDialect
    > jdbc.databaseurl=jdbc:mysql://192.168.80.128:3306
    > jdbc.username=contactmanager jdbc.password=1234

please help =(
4

1 回答 1

1

堆栈跟踪的这一部分让我怀疑您使用的是 Hibernate 4:

Caused by: java.lang.ClassCastException:     
org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider cannot be cast to org.hibernate.service.jdbc.connections.spi.ConnectionProvider

如果您使用的是hibernate4 ,则需要使用org.springframework.orm.hibernate4.LocalDataSourceConnectionProvider而不是。org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider

于 2012-10-04T11:08:18.733 回答