在ItemProcessor中发出自动装配Bean

时间:2017-05-15 17:52:34

标签: java hibernate spring-batch batch-processing autowired

我必须将服务自动装入我的ItemProcessor以从数据库中检索一个对象并进行一些处理,但它不起作用,当我尝试在{{1}中使用它时,我总是得到java.lang.NullPointerException }}

以下是上下文模型:

ItemProcessor

这是我的流程类

<import resource="classpath:context-datasource.xml"/>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />

    <property name="annotatedClasses">
        <list>
            <value>ma.controle.gestion.modele.Batch</value>
            <value>ma.controle.gestion.modele.Ressource</value>
        </list>

    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.enable_lazy_load_no_trans">true</prop> 
        </props>
    </property>
</bean>

<!-- Transaction Manager is defined -->
<bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />

这是我的服务

public class BatchItemProcessor implements ItemProcessor<Batch,Batch> {


@Autowired
@Qualifier("ressourceService")
private IRessourceService ressourceService;
   // getters setters   
@Override
public Batch process(Batch b) throws Exception {
return groupByTypeInstRub(batch);
}



    public Batch groupByTypeInstRub(Batch b){

    if(BatchFieldSetMapper.batchs.isEmpty()){
        return null;
    }else if(BatchFieldSetMapper.batchs.contains(b)){
        Double montantPaye=0.0;
        Double montantRetenu=0.0;

        for (Iterator<Batch> iterator = BatchFieldSetMapper.batchs.iterator(); iterator.hasNext();) {
            System.out.println("ressource Matricule  : " + b.getMatricule());
            Ressource ressource = (Ressource) ressourceService.findRessourceByMatricule(b.getMatricule());
            System.out.println("Ressource nom, " + ressource.getNom() + ", Prenom : " + ressource.getPrenom());
            Batch batch = (Batch) iterator.next();

            if(b.getInstitution().equalsIgnoreCase(batch.getInstitution()) && b.getType().equals(batch.getType()) && b.getRubrique().equalsIgnoreCase(batch.getRubrique())){
                montantPaye+=batch.getMontantPaye();
                montantRetenu+=batch.getMontantRetenu();
                iterator.remove();
            }
        }

        b.setMontantPaye(montantPaye);
        b.setMontantRetenu(montantRetenu);
        return b;
    }else
        return null;
}

}

这是我的ressourceDao

@Transactional
@Service("ressourceService")
public class RessourceServiceImpl implements IRessourceService {

@Autowired
@Qualifier("ressourceDao")
private RessourceDao ressourceDao;

@Override
public Ressource findRessourceByMatricule(String matricule) {
    System.out.println("Find ressource By Matricule, Dao");
    return ressourceDao.findRessourceByMatricule(matricule);
}
// getters setters

公共类RessourceDao扩展了GenericDaoHibernateImpl {

@Repository

这是错误的日志

@Autowired
@Qualifier("sessionFactory")
private SessionFactory sessionFactory;


@Autowired
public RessourceDao(@Qualifier("sessionFactory") SessionFactory sessionFactory){
    super(sessionFactory);
}


public Ressource findRessourceByMatricule(String matricule){
    System.out.println("Find ressource By Matricule, Dao");
    Ressource ressource=null;

    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Ressource.class);
    criteria.add(Restrictions.eq("matricule",matricule));

    List<Ressource> ressources = criteria.list();

    for (Ressource ressource2 : ressources) {
        System.out.println("ressource nom, prenom " + ressource2.getNom() + "," + ressource2.getPrenom());
    }

    if(ressources.equals(null)){
        return null;
    }else{
        ressource=ressources.get(0);    
    }
    return ressource;
}
}

我在web-inf中有另一个配置applicationContext但是当我运行批处理时,我访问了资源类路径中的这个配置。

0 个答案:

没有答案