2014-03-05 40 views
0

下面是注入sessionFactory的EmployeeDaoImpl文件。AnnotationConfiguration实例需要使用<myClass>

@Repository 公共类EmployeeDaoImpl实现EmployeeDao {

@Autowired 
    private SessionFactory sessionFactory; 

@Override 
public void addEmployee(TestEmployee employee) { 
    this.sessionFactory.getCurrentSession().save(employee); 
} 

@SuppressWarnings("unchecked") 
@Override 
public List<TestEmployee> getAllEmployees() { 
    return this.sessionFactory.getCurrentSession().createQuery("from TestEmployee").list(); 
} 

@Override 
public void deleteEmployee(Integer employeeId) { 
    TestEmployee employee = (TestEmployee) sessionFactory.getCurrentSession().load( 
      TestEmployee.class, employeeId); 
    if (null != employee) { 
     this.sessionFactory.getCurrentSession().delete(employee); 
    } 
} 

}

下面是我的员工的servlet文件

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

<context:annotation-config /> 
<context:component-scan base-package="com.rights.controller" /> 
<mvc:annotation-driven /> 


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

<bean id="messageSource" 
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basename" value="classpath:messages" /> 
    <property name="defaultEncoding" value="UTF-8" /> 
</bean> 
<bean id="propertyConfigurer" 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
    p:location="/WEB-INF/jdbc.properties" /> 

<bean id="dataSource" 
    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" 
    p:driverClassName="${jdbc.driverClassName}" 
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" 
    p:password="${jdbc.password}" /> 


<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="configLocation"> 
     <value>classpath:hibernate.cfg.xml</value> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
      <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 
</bean> 

<bean id="employeeDAO" class="com.rights.dao.EmployeeDaoImpl"></bean> 
<bean id="employeeManager" class="com.rights.services.EmployeeManagerImpl"></bean> 

<tx:annotation-driven transaction-manager="transactionManager"/> 
<bean id="transactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

</beans> 

我正在一个AnnotationConfiguration实例是必要的错误。我在网上搜索它,但无法解决这个问题。我有运行配置。我使用hibernate 3和spring 3.1 JARS。好心帮

回答

0

daoin

<context:component-scan base-package="com.rights.controller" /> 

你好像你使用忘记您的存储库的包,你应该添加:

<context:component-scan base-package="com.rights.controller com.rights.dao" /> 

在您发布的配置,注释@Repository类可不是春季后期处理,如果包装不包含在元器件扫描中

我希望它可以帮到你

+0

没有它不帮助我.. – rishi

+2

我自己解决了这个问题...刚添加的 org.hibernate.cfg.AnnotationConfiguration 在员工的servlet文件中的hibernateProperties以上。 – rishi

相关问题