【发布时间】:2014-10-04 09:24:08
【问题描述】:
解决了!!! 我将 context.xml 移动到 web-inf 文件夹并将 context-param 更改为:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context.xml</param-value>
</context-param>
“没有为依赖找到类型 [org.hibernate.SessionFactory] 的合格 bean..”当我尝试将我的类 User 保存到数据库时遇到异常。道类:
@Repository
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Autowired
private SessionFactory sessionFactory;
public void addUser(User user) {
sessionFactory.getCurrentSession().save(user);
}
}
我的 bean 定义在这里:resources/context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.connection.driver_class">org.postgresql.Driver</prop>
<prop key="hibernate.connection.url">jdbc:postgresql://localhost:5432/db</prop>
<prop key="hibernate.connection.username">user</prop>
<prop key="hibernate.connection.password">pw</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.wily.User</value>
</list>
</property>
</bean>
</beans>
你能告诉我我做错了什么吗?谢谢。
编辑:web.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app 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_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml, classpath:context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
-同样,context.xml 在资源文件夹中
【问题讨论】:
-
这是一个独立的应用程序还是作为 web 应用程序的一部分运行?如何初始化 Spring 容器?
-
它是webapp的一部分,问题好像是我不知道如何初始化容器,你能帮帮我吗?