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

new*_*ger 2 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?
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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> 
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢

Vir*_*zzo 6

它实际上是抱怨annotatedClasses会话工厂的属性,你使用的LocalSessionFactoryBean是没有它的(你已经注释掉AnnotationSessionFactoryBean确实有它).