Prerna
Prerna

Reputation: 111

Spring Bean is not writable or has an invalid setter method

I'm experimenting with Spring, I'm following the book: Spring: A developer's notebook. I'm getting this error:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'validateFile' defined in class path resource [batch-config.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'customersDAO' of bean class [com.emp.ValidateCustomer]: Bean property 'customersDAO' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

Below is my batch-config.xml:-

<bean id="validateFile" class="com.emp.ValidateCustomer" >

    <property name="customersDAO">
        <bean class="com.emp.dao.CustomerDao">
                <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    </property>

     <bean id="sessionFactory"  class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="customerdb"/>
    <property name="packagesToScan" value="com.emp" />
    <property name="hibernateProperties">
        <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
                <prop key="hibernate.jdbc.batch_size">50</prop>
                <prop key="hibernate.default_batch_fetch_size">50</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.use_sql_comments">false</prop>
                <prop key="hibernate.generate_statistics">false</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">false</prop>
                <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
            </props>
    </property>
</bean> 

</bean>

Below is CustomersDao.java:-

@Repository
public class CustomersDao {



    @SuppressWarnings("deprecation")
    private HibernateTemplate hibernateTemplate=null;



    @Autowired
    @Qualifier("sessionFactory")
    public void setSessionFactory(SessionFactory sessionFactory) {
        hibernateTemplate = new HibernateTemplate(sessionFactory);
}
}


And in ValidateCustomer.java:-

    @Autowired
    private CustomersDao customersDAO;

Please help.

Upvotes: 0

Views: 1844

Answers (1)

Roland Roberts
Roland Roberts

Reputation: 27

As @MarcosBarbero says, without seeing more of ValidateCustomer.java, it's impossible to be definitive about the problem.

What annotations are on the ValidateCustomer class? I have no problem with Autowired for a private property when the enclosing class is a component or service. If it's not, then do you have a public setter? The property is not public, if the setter is not public, Spring won't be able to set it.

Upvotes: 1

Related Questions