Spring managed bean injection in class implementing RowMapper

百般思念 提交于 2021-01-27 19:05:23

问题


I have a class BusinessRowMapper that implements RowMapper to convert PostGres JSONB object to Java object.

BusinessRowMapper implements RowMapper<PersonDetails>

it overrides mapRow

   public class BusinessRowMapper implements RowMapper<PersonDetails> {

    private PersonUtility utils;

    public BusinessRowMapper(PersonUtility utils) {
        super();
        this.utils = utils;
    }


    public PersonDetails mapRow(final ResultSet rs, final int rowNum) throws SQLException {
        PersonDetails personDetail = utils.unMarshallAndConvertData(rs
                .getString(ConstantsV4.COLUMN_CUST_DTLS_JSON_DOC));
        return personDetail;
    }
}

Now, How do I do Spring managed Injection of PersonUtility Bean in this BusinessRowMapper bean rather than passing the utility bean as constructor argument to BusinessRowMapper?

getNamedParameterJdbcTemplate().query(
                        ConstantsV4.RETRIEVE_PERSON_DETAIL_QUERY, params,
                        new BusinessRowMapper(utility));

回答1:


You can define PersonUtility class as spring bean adding @component over the class.

Then you can autowire the field in BusinessRowMapper

@Component 
    public class BusinessRowMapper implements RowMapper<PersonDetails> { 

    @Autowired 
    private PersonUtility utils; 


    ... 


    } 


来源:https://stackoverflow.com/questions/44061320/spring-managed-bean-injection-in-class-implementing-rowmapper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!