开发者

In spring, how to pass objects from one bean to another bean?

I have a row mapper class which implements RowMapper interface. I need to implement the mapRow method in that. The arguments of it are ResulSet and index. I would like to use this ResultSet object in another bean开发者_开发知识库. How do i get it there ?


Set this object as instance variable. But i would never recommend that for ResultSet. Resultset will be useless once its closed by spring (as spring is managing it).

Better extract the data out of ResultSet store the data as some model bean in the instance variable (but keep in mind, by default the beans are singleton, and for each execution storing the data as instance variable would not make much sense either).

EDIT-- okay to refine a bit more, i am putting an example here

// this is singleton by default, if you want to store data in this bean
// mark it as bean either by annotation or thorugh xml
// so that it can be accessed through spring context
public class MyBeanRowMapper implements RowMapper
{
    // you can store something here, 
    // but with each execution of mapRow, it will be updated
    // not recommended to store execution result here
    // or inject other bean here // SomeOtherBean someOtherbean;

    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        MyBean myBean = new MyBean();
        myBean.setId(rs.getInt("BEAN_ID"));
        myBean.setName(rs.getString("NAME"));
        // set this bean into gloablly accessible object here
        // or to the bean in which you want to access the result
        // something like -->// someOtherBean.setMyBean(myBean)
        // again be careful if someOtherBean is singleton by default

        return myBean;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜