使用<myclass> </myclass>需要AnnotationConfiguration实例

时间:2014-03-05 09:20:46

标签: spring hibernate spring-mvc

下面是注入sessionFactory的EmployeeDaoImpl文件。

@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); 
    } 
} 

}

以下是我的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>

我收到An AnnotationConfiguration实例是必需的错误。我在网上搜索过但无法解决这个问题。我有运行配置。我正在使用hibernate 3和spring 3.1 JARS。请帮助

1 个答案:

答案 0 :(得分:0)

daoin

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

您似乎忘记了存储库的包,您应该使用以下方法添加它:

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

在您发布的配置中,带有注释@Repository的类可能不会被spring进行后处理,如果它包不包含在组件扫描中

我希望它可以帮到你