[英]could not autowire sessionFactory
I can't autowire sessionFactory bean defined in context.xml: 我无法自动连接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>
maybe other cfg files can be helpful, web.xml: 也许其他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>
dispatcher-servlet.xml: dispatcher-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>
what can be wrong? 有什么问题吗? i have all 3 files in the same directory, thanks for advices. 我在同一目录中拥有所有3个文件,谢谢您的建议。 EDIT: stack trace: http://pastebin.com/tY87DgLb 编辑:堆栈跟踪: http : //pastebin.com/tY87DgLb
Since your servlet is named web-dispatcher
and your servlet xml file is web-dispatcher-servlet.xml
, you don't need to specify it in configLocation
, Spring will find it in classpath using [servletName]-servlet.xml
. 由于您的servlet名为web-dispatcher
而servlet xml文件为web-dispatcher-servlet.xml
,因此您无需在configLocation
指定它,Spring会使用[servletName]-servlet.xml
在类路径中找到它。 But you need to specify context.xml
, which Spring needs to know about(in order to configure sessionFactory bean, and other beans). 但是您需要指定context.xml
,Spring需要知道该上下文(以便配置sessionFactory bean和其他bean)。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:context.xml</param-value>
</context-param>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.