将'$ Proxy0类型的属性值转换为必需类型的失败意味着什么?

时间:2013-06-04 08:55:29

标签: spring spring-batch

我在Spring Batch代码中遇到错误,该错误说: 在类路径资源中定义名为'itemReader'的bean时出错,无法将类型'$ Proxy0的属性值转换为属性'preparedStatementSetter'所需的类型。

这是我的ItemReader代码:

<beans:bean id="itemReader"
 class="org.springframework.batch.item.database.JdbcCursorItemReader">
    <beans:property name="dataSource" ref="dataSource"/>
    <beans:property name="sql" value="select * from Trans_Tst WHERE id =?"/>
    <beans:property name="rowMapper" ref="romMapper"/>
    <beans:property name="preparedStatementSetter" ref="ReaderSqlParameterSetter"/>
</beans:bean>



<beans:bean id="ReaderSqlParameterSetter"
 class="sa.com.anb.itg.dev.settlement.batch.ReaderSqlParameterSetter" scope="step">
<beans:property name="id" value="#{jobParameters[id]}"/>
</beans:bean>

这是我的ReaderSqlParameterSetter类:

package sa.com.anb.itg.dev.settlement.batch;
import java.sql.PreparedStatement;
import org.springframework.batch.item.database.ItemPreparedStatementSetter;
import java.sql.SQLException;

public class ReaderSqlParameterSetter implements
 ItemPreparedStatementSetter<transactionas>{

    public void setValues(transactionas transactionas,PreparedStatement ps) 
          throws SQLException {
        ps.setInt(1, transactionas.GetID());

    } 

}

<beans:bean id="jdbcWriter" 
  class="org.springframework.batch.item.database.JdbcBatchItemWriter">
    <beans:property name="dataSource" ref="dataSource"/> 
    <beans:property name="sql" value="update Trans_Tst set ()amount) values (?) 
           where id= ?"/>
    <beans:property name="preparedStatementSetter" 
          ref="WriterSqlParameterSetter"/> 
</beans:bean>

<beans:bean id="WriterSqlParameterSetter" 
  class="sa.com.anb.itg.dev.settlement.batch.WriterSqlParameterSetter" scope="step">
    <beans:property name="amount" value="#{jobParameters[amount]}"/>
    <beans:property name="id" value="#{jobParameters[id]}"/>  
</beans:bean>



    public class WriterSqlParameterSetter implements PreparedStatementSetter {

    transactionas transactionas = new transactionas();

    public void setValues(PreparedStatement ps) throws SQLException {
        ps.setDouble(1, transactionas.GetAmount());
        ps.setInt(2, transactionas.GetID());

    } 

}

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

您没有使用正确的界面。您的ReaderSqlParameterSetter应该实现 PreparedStatementSetter 而不是 ItemPreparedStatementSetter

如果你看一下JdbcCursorItemReader的impl:

public class JdbcCursorItemReader<T> extends AbstractCursorItemReader<T> {

PreparedStatement preparedStatement;

PreparedStatementSetter preparedStatementSetter;

String sql;

RowMapper rowMapper;

该属性是PreparedStatementSetter。

如果你看一下你尝试使用的界面:

public interface ItemPreparedStatementSetter<T> {
/**
 * Set parameter values on the given PreparedStatement as determined from
 * the provided item.
 * @param ps the PreparedStatement to invoke setter methods on
 * @throws SQLException if a SQLException is encountered (i.e. there is no
 * need to catch SQLException)
 */
void setValues(T item, PreparedStatement ps) throws SQLException;

}

此接口不扩展PreparedStatementSetter ...

所以它不起作用。如果你没有放置scope = step,你可能会遇到ClassCastException错误!