Java 行映射器中的Autowire对象映射器
我发现可以在rowmapper中自动连线 如果您希望ScoreMapper实例向ScoreCreator ScoreCreator注入Springbean,则ScoreMapper实例本身必须是Springbean,即由Spring创建和管理 或加入 您可以将PersonUtility类定义为Springbean,在该类上添加@component 但当前行映射器是在Java 行映射器中的Autowire对象映射器,java,spring,jdbctemplate,rowmapper,Java,Spring,Jdbctemplate,Rowmapper,我发现可以在rowmapper中自动连线 如果您希望ScoreMapper实例向ScoreCreator ScoreCreator注入Springbean,则ScoreMapper实例本身必须是Springbean,即由Spring创建和管理 或加入 您可以将PersonUtility类定义为Springbean,在该类上添加@component 但当前行映射器是在jdbcTemplate中用new实例化的。query: jdbcTemplate.query(SQL, new Object[]
jdbcTemplate中用new
实例化的。query
:
jdbcTemplate.query(SQL, new Object[] {}, new MyRowMapper())
我无法在内部自动连接Spring管理的ObjectMapper
public class MyRowMapper implements RowMapper<Map<Integer, Type>> {
@Autowired
@Qualifier("myObjectMapper")
ObjectMapper objectMapper;
公共类MyRowMapper实现了RowMapper{
@自动连线
@限定符(“myObjectMapper”)
对象映射器对象映射器;
我应该如何重构当前代码来管理bean行映射器?行映射器是一个线程安全的类。这意味着,它的单个实例可以在多个线程之间共享。因此,这意味着,您可以让它成为一个单例类,让spring处理它的生命周期(使用@Component之类的注释之一)。无论您想在何处使用它的实例,只需自动连接/注入现有实例,而不是每次都实例化它(
new
)
参考这一点。它在类似的行中您是否将MyRowMapper注释为@Component?如果是,您是否可以发布您得到的异常?
@Component
public class MyRowMapper implements RowMapper<Map<Integer, Type>> {
@Autowired
@Qualifier("myObjectMapper")
ObjectMapper objectMapper;
class ASingletonClass(){
@Autowired MyRowMapper myRowMapper;
public MyRowMapper myAweSomeMethod(){
return jdbcTemplate.query(SQL, new Object[] {}, myRowMapper)
}
}