@Autowired 不适用于从非 spring jpos 库实现的 class

@Autowired does not work for class that are implementing from non-spring jpos library

我在我的 spring 引导应用程序中使用带有事务管理器的 jpos Q2 服务器,但是当我尝试在我的 class 中实现 DI 时,它是从 Jpos TransactionParticipant 接口实现的,它给了我空指针异常。

根据我的理解,我已经尝试了 spring IoC 引导中可能存在的所有选项。似乎我无法在 Spring IoC/DI 模块中注册 TransactionParticipant 第三方库。

package com.fonepay.iso;

@Service("processIsoTxn")
public class ProcessIsoTxn implements TransactionParticipant{
@Autowired
    private CbsTxnService cbsTxnService;

    @Override
    public int prepare(long id, Serializable context) {
        Context ctx = (Context) context;
        try{

            ISOMsg request = (ISOMsg) ctx.get("REQUEST");

            //Call local processing Message
            //CbsTxnService cbsTxnService = new CbsTxnServiceImpl();
            ISOMsg response = cbsTxnService.createFinancialTxn(request);

            ctx.put("RESPONSE", response);
            return PREPARED;

        }catch(Exception ex){
            System.out.println("Process Iso Txn | error | "+ex);
        }

        return 0;
    }
}
package com.fonepay.service.impl;

@Service("cbsTxnService")
@Transactional
public class CbsTxnServiceImpl implements CbsTxnService{
     public ISOMsg createFinancialTxn(ISOMsg isoMsg) {...}
}
@SpringBootApplication
@ComponentScan("com.fonepay")
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class JposserverApplication {

    public static void main(String[] args) {
        SpringApplication.run(JposserverApplication.class, args);
    }
}

我经常java.lang.NullPointerException排队 ISOMsg 响应 = cbsTxnService.createFinancialTxn(请求);

  • 试试这个,替换@Autowired注解
  • 尝试使用构造函数
@Service("processIsoTxn")
public class ProcessIsoTxn implements TransactionParticipant{

 private CbsTxnService cbsTxnService;

  public ProcessIsoTxn (CbsTxnService cbsTxnService) {
    this.cbsTxnService = cbsTxnService;
  }

如果有人感兴趣,这就是我实现解决方法的方法,请参考:https://confluence.jaytaala.com/display/TKB/Super+simple+approach+to+accessing+Spring+beans+from+non-Spring+managed+classes+and+POJOs

private CbsTxnService getCbsTxnService() {
        return SpringContext.getBean(CbsTxnService.class);
    }

ISOMsg response = getCbsTxnService().createFinancialTxn(request);

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContext implements ApplicationContextAware {

    private static ApplicationContext context;

    /**
     * Returns the Spring managed bean instance of the given class type if it exists.
     * Returns null otherwise.
     * @param beanClass
     * @return
     */
    public static <T extends Object> T getBean(Class<T> beanClass) {
        return context.getBean(beanClass);
    }

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {

        // store ApplicationContext reference to access required beans later on
        SpringContext.context = context;
    }
}