gpt4 book ai didi

java - 如何在spring 3.0中注入(inject)map类型对象

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:11 24 4
gpt4 key购买 nike

在 DAO 中:

private Map<Integer,String> departments = new LinkedHashMap<Integer, String>();

@Override
public List<DepartmentEntity> getAllDepartments() {
return this.sessionFactory.getCurrentSession().createQuery("from DepartmentEntity de order by LOWER(de.departmentname)").list();

}

@Override
public Map<Integer, String> loadDepartments() {
departments.clear();
for (DepartmentEntity de : getAllDepartments())
departments.put(de.getDepartmentid(), de.getDepartmentname());
return departments;
}

它工作正常,但在 Spring 手动创建对象时,它的代码很糟糕

private Map<Integer,String> departments;

那么,在我的例子中,如何从外部注入(inject)LinkedHashMap类型的 map 对象?

我尝试过,但遇到了空指针异常等异常

请任何人帮助我..

最佳答案

 <util:map id="myMap" map-class="java.util.LinkedHashMap" key-type="java.lang.Integer" value-type="java.lang.String"/>

<bean id="departmentDAOImpl" class="com.leadwinner.infra.assets.dao.DepartmentDAOImpl">
<property name="departments" ref="myMap"></property>
</bean>

关于java - 如何在spring 3.0中注入(inject)map类型对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24400557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com