setter的参数类型是否与getter的返回类型匹配?

时间:2012-04-16 10:22:56

标签: java spring hibernate spring-mvc

我正在尝试学习如何使用DAO和BO类,我收到以下错误

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [spring/database/Hibernate.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'annotatedClasses' of bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Bean property 'annotatedClasses' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

Hibernate.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-2.5.xsd">

<!-- Hibernate session factory -->
<bean id="sessionFactory" 
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">-->
<property name="dataSource">
  <ref bean="dataSource"/>
</property>

<property name="configLocation">    
    <value>classpath:spring/database/hibernate.cfg.xml
    </value>
</property>

<property name="hibernateProperties">
   <props>
     <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
     <prop key="hibernate.show_sql">true</prop>
   </props>
</property>

    <property name="annotatedClasses">
<list>
    <value>com.fexco.helloworld.web.model.Customer</value>
    <value>com.fexco.helloworld.web.model.Countries</value>
</list>
</property>

</bean>

DataSource.xml

<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-2.5.xsd">

<bean 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
    <value>/properties/database.properties</value>
</property>
</bean>

<bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>


 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="data" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
            <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean> 

我不知道错误的来源,因为我的countries.java和customer.java类中的getter和setter方法都是正确的!

有人知道什么是错的吗? (如果您需要查看任何其他类,只需添加评论,说明哪些类和il发布它们)

由于

2 个答案:

答案 0 :(得分:6)

实际上是在抱怨会话工厂的annotatedClasses属性,你使用的是没有它的LocalSessionFactoryBean(你已经注释掉AnnotationSessionFactoryBean 做了< / em>有它)。

答案 1 :(得分:1)

在实际使用没有此属性的LocalSessionFactoryBean时,您似乎想要设置最初来自AnnotationSessionFactoryBean的“annotatedClasses”属性。