0

I'm developing java,spring web application. I had some problems with adding DataSource to the DAO implement class which extends JdbcDaoSupport.

I searched through the internet and found similar solution.

DaoImpliment Class

@Repository
public class UserDAOImpl extends JdbcDaoSupport implements UserDAO {

@Autowired
private DataSource dataSource;

@PostConstruct
private void initialize() {
    setDataSource(dataSource);
}

public int getUserID(String userName) {
    //testing JDBC
    String sql = "select user_id from  users where username =" + "'" + userName + "'";
    List<UserDTO> userDTOs = new ArrayList<UserDTO>();

    List<Map<String, Object>> rows = getJdbcTemplate().queryForList(sql);
    for (Map row : rows) {
        UserDTO userDTO = new UserDTO();
        userDTO.setUserID((Integer) row.get("CUST_ID"));
        userDTOs.add(userDTO);
    }

    int userID = userDTOs.get(0).getUserID();

    return userID;
}
}

and my bean configaration.

<bean id="userDao" class="com.avers.dao.UserDAOImpl">
    <property name="dataSource" ref="dataSource" />
</bean>

    <bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/aversdb"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
</bean>

but this gives me the error. org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException

I think the error is in bean configuration. I'm new to spring and someone please help.

Guest
  • 53
  • 2
  • 15

2 Answers2

0

I can see you @Autowired DataSource as well as provided as a reference property in the same UserDAOImpl class which is causing NestedServletException. You can be avoid it in 2 ways:

  1. Remove @Autowired from DataSource in your UserDAOImpl class as below:

    @Repository
    public class UserDAOImpl extends JdbcDaoSupport implements UserDAO {
    
    private DataSource dataSource;
    
    @PostConstruct
    private void initialize() {
      setDataSource(dataSource);
    }
    
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
    
    public int getUserID(String userName) {
        //testing JDBC
        String sql = "select user_id from  users where username =" + "'" + userName + "'";
        List<UserDTO> userDTOs = new ArrayList<UserDTO>();
    
        List<Map<String, Object>> rows = getJdbcTemplate().queryForList(sql);
        for (Map row : rows) {
            UserDTO userDTO = new UserDTO();
            userDTO.setUserID((Integer) row.get("CUST_ID"));
            userDTOs.add(userDTO);
        }
    
        int userID = userDTOs.get(0).getUserID();
    
        return userID;
      }
    }
    
  2. Or remove <property name="dataSource" ref="dataSource" /> from your bean configuration file as below:

    <bean id="userDao" class="com.avers.dao.UserDAOImpl"/>
    <bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/aversdb"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
    </bean>
    
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
0

@Autowired annotation manages initialization instead of you. When an object that contains a field, that is @Autowired is created, all @Autowired fields are created.

That means, that you don't need initialization method below.

Please comment and tell me if this worked. More information can be found: http://www.mkyong.com/spring/spring-auto-wiring-beans-with-autowired-annotation/

xenteros
  • 15,586
  • 12
  • 56
  • 91