问题
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