简体   繁体   中英

autowire DataSource to a class which extends JdbcDaoSupport

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.

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> 

@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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM