Announcement Announcement Module
Collapse
No announcement yet.
org.springframework.beans.factory.BeanCreationExce ption Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • org.springframework.beans.factory.BeanCreationExce ption

    Hi,
    I'm newbie and I'm working on a Spring JDBC DaoSupport project.
    I have a question about the org.springframework.beans.NotWritablePropertyExcep tion and the org.springframework.beans.factory.BeanCreationExce ption.

    I can not seem to wrap my head around my issue.
    I have read a lot of forums and can not seem to get my code right.
    I think I have the right setters and getters, so why is my code still not working?

    The code is quite simple:

    applicationContext.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schem...ng-aop-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://xxxxxxxxxxx" /> <!-- localhostort/db -->
    <property name="username" value="xxxxxx" />
    <property name="password" value="xxxxxxx" />
    </bean>

    <bean id ="jdbcTemplate" class ="org.springframework.jdbc.core.JdbcTemplate">
    <property name ="dataSource" ref ="dataSource"/>
    </bean>

    <bean id="userDao" class="dao.impl.UserDAOImpl" >
    <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <bean id="userService" class="model.UserService" >
    <property name="userDao" ref="userDao"></property> This bean is causing me trouble.
    </bean>

    </beans>

    Error (there are several of the same error listed):

    SEVERE: Context initialization failed
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'userService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'userDao' of bean class [model.UserService]: Bean property 'userDao' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?


    My DAOImpl class (a little example):
    package dao.impl;

    import java.util.List;

    import model.User;
    import model.UserRowMapper;

    import org.springframework.jdbc.core.BeanPropertyRowMappe r;
    import org.springframework.jdbc.core.support.JdbcDaoSuppo rt;

    import dao.UserDAO;


    public class UserDAOImpl extends JdbcDaoSupport implements UserDAO {


    public UserDAOImpl(){

    }

    @Override
    public void insertUser(User user) {

    System.err.println("Database -> insert -> " + user.getMail());
    String sql = "INSERT INTO USER " +
    "(MAIL, PASSWORD, FIRSTNAME, LASTNAME, TLF, CITY, SHOWMAIL, SHOWTLF,SHOWFIRSTNAME, SHOWLASTNAME)"+
    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

    getJdbcTemplate().update(sql, new Object[] { user.getMail(),
    user.getPassword(),user.getFirstName(), user.getLastName(), user.getTlf(), user.getCity(),
    user.getShowMail(), user.getShowTlf(), user.getShowFname(), user.getShowLname()
    });

    }

    My service class:
    package model;

    import dao.impl.UserDAOImpl;

    public class UserService {

    private UserDAOImpl userDao;

    public UserDAOImpl getUserDao() {
    return userDao;
    }

    public void setUserDao(UserDAOImpl userDao) {
    this.userDao = userDao;
    }

    public void addUser(User user) {
    getUserDao().insertUser(user);
    }

    }


    Please help me get on track. I'm truly grateful for any response
    Last edited by Sharay; Apr 4th, 2013, 08:17 AM.

  • #2
    Try rewriting the Service class to use the DAO interface.
    Spring encourages you to use interfaces in your code, and implementations in the configuration of beans.
    Code:
    package model;
    
    import dao.impl.UserDAO;
    
    public class UserService {
    
    private UserDAO userDao;
    
    public UserDAO getUserDao() {
    return userDao;
    }
    
    public void setUserDao(UserDAO userDao) {
    this.userDao = userDao;
    }
    
    public void addUser(User user) {
    getUserDao().insertUser(user);
    }
    
    }

    Comment

    Working...
    X