Spring单例作用域的bean对于不同的对象具有不同的属性值

时间:2018-07-23 21:49:53

标签: java spring

我的应用程序基于Spring + SockJs构建。主页面表示可用连接表,以便用户可以实时监视它们。每个单独的URL监视器都可以彼此独立地挂起/恢复。问题是一旦您暂停了某个监视器,便永远无法恢复它,因为MonitoringFacade bean的ApplicationEvents属性对于单个实体突然变为null。对于其他实体,听众保持良好的状态。尝试调用此类空侦听器的方法时,永远不会抛出NullPointerException。

class IndexController implements ApplicationEvents
...
public IndexController(SimpMessagingTemplate simpMessagingTemplate, MonitoringFacade monitoringFacade) {
        this.simpMessagingTemplate = simpMessagingTemplate;
        this.monitoringFacade = monitoringFacade;
    }

    @PostConstruct
    public void initialize() {
        if (logger.isDebugEnabled()) {
            logger.debug(">>Index controller initialization.");
        }

        monitoringFacade.addDispatcher(this);
    }

...

@Override
    public void monitorUpdated(String monitorId) {
        if (logger.isDebugEnabled()) {
            logger.debug(">>Sending monitoring data to client with monitor id " + monitorId);
        }

        try {
            ConfigurationDTO config = monitoringFacade.findConfig(monitorId);
            Report report = monitoringFacade.findReport(monitorId);
            ReportReadModel readModel = ReportReadModel.mapFrom(config, report);
            simpMessagingTemplate.convertAndSend("/client/update", readModel);
        } catch (Exception e) {
            logger.log(Level.ERROR, "Exception: ", e);
        }
    }

public class MonitoringFacadeImpl implements MonitoringFacade
...
private ApplicationEvents dispatcher;

  public void addDispatcher(ApplicationEvents dispatcher) {
        logger.info("Setting up dispatcher");
        this.dispatcher = dispatcher;
    }

...
 @Override
    public void refreshed(RefreshEvent event) {
        final String monitorId = event.getId().getIdentity();
        if (logger.isDebugEnabled()) {
            logger.debug(String.format(">>Refreshing monitoring data with monitor id '%s'", monitorId));
        }

        Configuration refreshedConfig = configurationService.find(monitorId);
        reportingService.compileReport(refreshedConfig, event.getData());

        if (logger.isDebugEnabled()) {
            logger.debug(String.format(">>Notifying monitoring data updated with monitor id '%s'", monitorId) + dispatcher);
        }
        dispatcher.monitorUpdated(monitorId); // here dispatcher has null value... or it's actually not

    }

void refreshed(RefreshEvent event)方法成功通过接口从Quartz调度程序接收更新并将其发送回控制器。

问题是,单例作用域的bean如何针对其所应用的不同对象具有不同的属性值,以及即使我从未将其设置为null,为什么这样的属性也会变为null?

UPD:

@MessageMapping("/monitor/{monitorId}/suspend")
    public void handleSuspend(@DestinationVariable String monitorId) {
        if (logger.isDebugEnabled()) {
            logger.debug(">>>Handling suspend request for monitor with id " + monitorId);
        }

        try {
            monitoringFacade.disableUrlMonitoring(monitorId);
            monitorUpdated(monitorId);// force client update
        } catch (Exception e) {
            logger.log(Level.ERROR, "Exception: ", e);
        }
    }

    @MessageMapping("/monitor/{monitorId}/resume")
    public void handleResume(@DestinationVariable String monitorId) {
        if (logger.isDebugEnabled()) {
            logger.debug(">>>Handling resume request for monitor with id " + monitorId);
        }

        try {
            monitoringFacade.enableUrlMonitoring(monitorId);
            monitorUpdated(monitorId);// force client update
        } catch (Exception e) {
            logger.log(Level.ERROR, "Exception: ", e);
        }
    }

0 个答案:

没有答案