弹簧自定义通用转换器不起作用



在我的Spring MVC Web应用程序中,我使用通用转换器,将字符串(id)转换为公司,通过使用(Service and dao)组件

将字符串(id)转换为公司

首先,在我的MVC-Config中,我添加转换器如下:

@Override
public void addFormatters(FormatterRegistry registry) {
    registry.addConverter(new GenericIdToCompanyConverter(new CompanyServiceImp()));
}

CompanyService

@Service
@Transactional
@Qualifier("companyService")
public class CompanyServiceImp implements ICompanyService {
    @Resource
    @Qualifier("companyDAO")
    private ICompanyDao dao;

    public void setDao(ICompanyDao dao) {
        this.dao = dao;
    }
    @Override
    public Company find(Long id) throws BusinessException {
        Company current = dao.find(id);
        if(current == null) {
            throw new BusinessException("notFound");
        }
        return current;
    }
....
}

通用转换器:

public class GenericIdToCompanyConverter implements GenericConverter {
    private ICompanyService companyService;
    public GenericIdToCompanyConverter(ICompanyService companyService) {
        super();
        this.companyService = companyService;
    }
    @Override
    public Set<ConvertiblePair> getConvertibleTypes() {
        ConvertiblePair[] pairs = new ConvertiblePair[] { new ConvertiblePair(Number.class, Company.class), new ConvertiblePair(String.class, Company.class) };
        return ImmutableSet.copyOf(pairs);
    }
    @Override
    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
        long id = 0;
        if( sourceType.getType() == String.class) {
            try {
                id = Long.valueOf((String) source);
            }catch(NumberFormatException e) {
                return null;
            }
        }else if( sourceType.getType() == Number.class) {
            id = (Long) source;
        }else {
            return null;
        }
        try {
            return companyService.find(Long.valueOf(id));
        } catch (BusinessException e) {
            return null;
        }
    }
}

以及接收数据表格的控制器(通过AJAX请求)

public @ResponseBody JsonResponseBean applay(@Valid VoucherForm form, BindingResult result, Locale locale) throws BusinessException {
....
}

保证有这些属性

public class VoucherForm{
    protected Long id;
    protected Company company;
...
}

当我运行应用程序和调用控制器方法时,它会返回公司属性的类型不匹配错误当我在调试模式下执行此操作时,我会发现它在ServiceCompany -dao.find(id)statment上失败了,我的dao == null

请帮助

最后,我必须自动自动converter

mvc-config....

@Autowired
private GenericIdToCompanyConverter genericIdToCompanyConverter;
@Override
public void addFormatters(FormatterRegistry registry) {
    registry.addConverter(genericIdToCompanyConverter);
}

并如下更新转换器:

public class GenericIdToCompanyConverter implements GenericConverter {
    @Resource
    @Qualifier("companyService")
    private ICompanyService companyService;
    @Override
    public Set<ConvertiblePair> getConvertibleTypes() {
        ....
    }
    @Override
    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
       ....
    }
}

最新更新