【问题标题】:could not autowire sessionFactory无法自动装配 sessionFactory
【发布时间】:2014-06-28 10:05:01
【问题描述】:

我无法自动装配 context.xml 中定义的 sessionFactory bean:

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

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.connection.url">jdbc:postgresql://localhost:8080/come_to_blog_db</prop>
                <prop key="hibernate.connection.driver_class">org.postgresql.Driver</prop>
                <prop key="hibernate.connection.username">postgres</prop>
                <prop key="hibernate.connection.password">admin</prop>
            </props>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>com.lime.model.User</value>
            </list>
        </property>
    </bean>

</beans>

也许其他 cfg 文件会有所帮助,web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


    <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>web-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>web-dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/web-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

调度程序-servlet.xml:

<?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: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-3.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

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

    <mvc:annotation-driven />
    <mvc:default-servlet-handler />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

有什么问题吗?我将所有 3 个文件都放在同一个目录中,谢谢您的建议。 编辑:堆栈跟踪:http://pastebin.com/tY87DgLb

【问题讨论】:

  • I can't autowire 是什么意思完全。提供错误和堆栈跟踪。
  • 请显示你的代码,你在哪里自动装配 sessionfactory 和你得到的堆栈跟踪。
  • 这里是堆栈跟踪:pastebin.com/tY87DgLb

标签: java xml spring hibernate postgresql


【解决方案1】:

由于您的 servlet 名为 web-dispatcher 并且您的 servlet xml 文件为 web-dispatcher-servlet.xml,因此您无需在 configLocation 中指定它,Spring 将使用 [servletName]-servlet.xml 在类路径中找到它。但是您需要指定 context.xml,Spring 需要知道它(以便配置 sessionFactory bean 和其他 bean)。

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:context.xml</param-value>
</context-param>

【讨论】:

  • 我已经添加了监听器并将 session-factory.xml 更新到 context.xml,但我还没有其他 bean。
  • 我更新了我的帖子,但我有一个问题,我应该将什么导入 contextConfigLocation(在 context-param 标签中)、dispetcher 或 context???
  • 因为 Spring ContextLoaderListener 需要了解您的 xml 上下文文件。我更新了我的帖子
  • 谢谢,它似乎已经解决了,但我得到了另一个错误:/,警告,maven 严重,然后异常,我花了 2 天时间配置 webapp,但我仍然无法连接我的数据库: /pastebin.com/pPxzbpiE
  • Jesus Christ cmon 杀了我,我没有注意到我在写错误的 connection.url,我输入的是 8080 而不是 5432,awrh.. 但是你能检查我的另一个问题吗?看来我终于连接上了我的数据库,但是:org.hibernate.HibernateException: createSQLQuery is not valid without active transaction
猜你喜欢
  • 1970-01-01
  • 2018-12-27
  • 1970-01-01
  • 1970-01-01
  • 2018-11-02
  • 1970-01-01
  • 1970-01-01
  • 2014-12-17
  • 1970-01-01
相关资源
最近更新 更多