依赖注入和从XML文件加载对象数据

时间:2019-01-31 15:05:06

标签: java spring dependency-injection

Spring框架如何帮助我创建从XML文件中加载数据的对象?我是Spring的新手,我想重构我的代码以对我的业务对象使用依赖注入。这里真的需要使用DI吗?

负责获取数据的方法:

    private static List<Expenditure> fetchExpendituresFromXMLElement(List<Element> expenditureElements) {
        List<Expenditure> expenditures = new ArrayList<>();
        expenditureElements.forEach(expenditure -> {
            String[] dateString = expenditure.getChild("date").getValue().split("-");
            String moneyStr[] = expenditure.getChild("money").getValue().split(" ");
            try {
                expenditures.add(Expenditure.createExpenditure(expenditure.getChild("description").getValue(),
                        Money.of(Double.valueOf(moneyStr[1]), Monetary.getCurrency(moneyStr[0])),
                        LocalDate.of(Integer.valueOf(dateString[2]), Integer.valueOf(dateString[1]),
                                Integer.valueOf(dateString[0])),
                        ExpenditureType.valueOf(expenditure.getChild("type").getValue())));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return expenditures;
    }

支出类别

public class Expenditure implements Comparable<Expenditure> {
    private String description;
    private MonetaryAmount monetaryAmount;
    private LocalDate date;
    private ExpenditureType type;
...
}

0 个答案:

没有答案