我启动了一个非常简单的应用程序,但似乎无法创建 sessionFactory bean,或者至少无法访问它。如您所见,我有我的 web.xml、我的 root-context.xml 和一个 dao。访问 dao 时, sessionFactory 为空。在这一点上,一切都在工作。
<?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">
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></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>
</web-app>
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<context:annotation-config />
<mvc:annotation-driven />
<context:component-scan base-package="com.acts542.mygunshop.*" />
<mvc:interceptors>
<bean class="com.acts542.mygunshop.controller.SecurityInterceptor" />
</mvc:interceptors>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mygunshop"/>
<property name="username" value="mygunshop"/>
<property name="password" value="sen32164"/>
</bean>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan">
<array>
<value>com.acts542.mygunshop.model</value>
<value>com.acts542.mygunshop.dao</value>
<value>com.acts542.mygunshop.service</value>
<value>com.acts542.mygunshop.controller</value>
</array>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">create</prop> -->
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
package com.acts542.mygunshop.dao;
// Generated Sep 12, 2013 11:27:13 AM by Hibernate Tools 3.4.0.CR1
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.acts542.mygunshop.model.Employees;
import static org.hibernate.criterion.Example.create;
/**
* Home object for domain model class Employees.
* @see com.acts542.mygunshop.model.Employees
* @author Hibernate Tools
*/
@Repository
@Transactional
public class EmployeesHome
{
private static final Log log = LogFactory.getLog(EmployeesHome.class);
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
public Session getSession()
{
return sessionFactory.getCurrentSession();
}
public void persist(Employees transientInstance)
{
log.debug("persisting Employees instance");
try
{
sessionFactory.getCurrentSession().persist(transientInstance);
log.debug("persist successful");
}
catch (RuntimeException re)
{
log.error("persist failed", re);
throw re;
}
}
public void attachDirty(Employees instance)
{
log.debug("attaching dirty Employees instance");
try
{
this.getSession().saveOrUpdate(instance);
log.debug("attach successful");
}
catch (RuntimeException re)
{
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Employees instance)
{
log.debug("attaching clean Employees instance");
try
{
sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
}
catch (RuntimeException re)
{
log.error("attach failed", re);
throw re;
}
}
public void delete(Employees persistentInstance)
{
log.debug("deleting Employees instance");
try
{
sessionFactory.getCurrentSession().delete(persistentInstance);
log.debug("delete successful");
}
catch (RuntimeException re)
{
log.error("delete failed", re);
throw re;
}
}
public Employees merge(Employees detachedInstance)
{
log.debug("merging Employees instance");
try
{
Employees result = (Employees) sessionFactory.getCurrentSession().merge(detachedInstance);
log.debug("merge successful");
return result;
}
catch (RuntimeException re)
{
log.error("merge failed", re);
throw re;
}
}
public Employees findById(java.lang.String id)
{
log.debug("getting Employees instance with id: " + id);
try
{
Employees instance = (Employees) sessionFactory.getCurrentSession().get(
"com.acts542.mygunshop.dao.Employees", id);
if (instance == null)
{
log.debug("get successful, no instance found");
}
else
{
log.debug("get successful, instance found");
}
return instance;
}
catch (RuntimeException re)
{
log.error("get failed", re);
throw re;
}
}
public List<Employees> findByExample(Employees instance)
{
log.debug("finding Employees instance by example");
try
{
List<Employees> results = (List<Employees>) sessionFactory.getCurrentSession()
.createCriteria("com.acts542.mygunshop.dao.Employees").add(create(instance)).list();
log.debug("find by example successful, result size: " + results.size());
return results;
}
catch (RuntimeException re)
{
log.error("find by example failed", re);
throw re;
}
}
}
我整天都在这。尝试了每个示例。我错过了什么