如何用singleton范围修改类(Spring)?

时间:2013-12-15 03:04:18

标签: java spring singleton

假设我正在尝试删除存储在ClassA的hashmap中的对象。 ClassB是一个静态类,它有一个destroy方法从hashmap中删除所需的对象。即使在删除它之后,当我尝试删除另一个对象时,我看到之前删除的旧元素仍然存在。有什么我想念的吗?

@Named
@Scope
public class ClassA implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Map<String, ModelObject> modelObjects = new HashMap<String, ModelObject>();

    public synchronized void addMo(ModelObject modelObject) {
        modelObjects.put(modelObject.getUuid(), modelObject);
    }

    public synchronized ModelObject updateMo(ModelObject modelObject) {
        return modelObjects.put(modelObject.getUuid(), modelObject);
    }

    public synchronized void removeMo(ModelObject modelObject) {
        modelObjects.remove(modelObject.getUuid());
    }

    public synchronized void removeMo(String modelObjectId) {
        modelObjects.remove(modelObjectId);
    }

    public ModelObject getMo(String uuid) {
        return modelObjects.get(uuid);
    }
}

ClassB

public class ClassB{

    private static ClassA manager;

    @Autowired(required = true)
    public void setmanager(ClassA manager) {
        ClassB.manager = manager;
    }
    public static synchronized void destoryMo(String modelObjectID) {
        // get ClassA bean
        if (manager == null) {
            LOG.error("Managed bean ClassA not found (null) ==> no modelObject update!");
        }
        ModelObject wb = manager.getMo(modelObjectID);
        if(wb != null){
            wb.removeAllCache();
            wb = null;
            manager.removeMo(modelObjectID);
        }
        LOG.info("Destroyed ModelObject with modelObject id :" + modelObjectID);

    }
}

0 个答案:

没有答案