maven util 类 添加 service

时间:2023-01-28 17:11:32

直接关键代码:

public class DictionaryUtil {
// 以下的处理,是为了在工具类中自动注入service
// 前提是在applicationContext.xml中,将该类的lazy-init设置为false
private static DictionaryUtil dictionaryUtil; @Autowired
private DicItemService dicItemService; @PostConstruct
public void init() {
dictionaryUtil = this;
dictionaryUtil.dicItemService = this.dicItemService;
}
public static Map<String, String> get(int dicId) {
int pageSize = 1000;
Page<DicItem> dicItemPage = new Page<>(0, pageSize); Page<DicItem> resultDicItemPage = dictionaryUtil.dicItemService.searchDicItemByPage(dicItemPage, dicId, "", 0);
List<DicItem> dicItems = resultDicItemPage.getResult();
Map<String, String> resultMap = new HashMap<>();
if (dicItems != null && dicItems.size() > 0) {
for (int i = 0; i < dicItems.size(); i++) {
DicItem dicItem = dicItems.get(i);
resultMap.put(dicItem.getItemCode(), dicItem.getItemValue());
}
}
return resultMap;
}
}

在 配置 bean 的地方加上:

<bean id="DictionaryUtil" class="cn.yiyizuche.util.DictionaryUtil" lazy-init="false"></bean>