RowMapper is a thread safe class. That means, it's single instance can be shared amongst multiple threads. So, that means, you can let it be a singleton class and let spring handle it's lifecycle (Using one of those annotation like @Component). And wherever you want to use it's instance, just autowire/inject existing instance rather than instantiating it every time (new
)
@Component
public class MyRowMapper implements RowMapper<Map<Integer, Type>> {
@Autowired
@Qualifier("myObjectMapper")
ObjectMapper objectMapper;
And then
class ASingletonClass(){
@Autowired MyRowMapper myRowMapper;
public MyRowMapper myAweSomeMethod(){
return jdbcTemplate.query(SQL, new Object[] {}, myRowMapper)
}
}
Refer this Answer. It's in similar lines
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…