gpt4 book ai didi

java - Spring 2.5.6 JMX NotificationPublisher 调用抛出 NPE

转载 作者:太空宇宙 更新时间:2023-11-04 12:18:58 26 4
gpt4 key购买 nike

我有时会遇到Caused by: java.lang.NullPointerException。看起来 spring 无法正确注入(inject) org.springframework.jmx.export.notification.NotificationPublisher 。我使用的是 spring 2.5.6 并部署在 WebSphere 7 中。我们如何解决这个 NPE 问题?

import javax.management.Notification;

import org.springframework.jmx.export.notification.NotificationPublisher;
import org.springframework.jmx.export.notification.NotificationPublisherAware;

import com.xyz.SpringUtil;
import com.xyz.notification.INotificationMBean;
import com.xyz.security.impl.SpringSecurityHelper;

/**
* provides a central class for the publishing of Notifications. This allows us to centralize all notification
* Listeners configuration in a central location, for this one MBean.
*/
public class NotificationMBean implements INotificationMBean, NotificationPublisherAware {

/** used to publish notifications. This will be set automatically by Spring */
private NotificationPublisher notificationPublisher;
private String proxyUser; // set by Spring - used for RMI-direct calls
private SpringSecurityHelper securityHelper;

protected SpringSecurityHelper getSecurityHelper() {
if (securityHelper == null) {
securityHelper = (SpringSecurityHelper) SpringUtil.getApplicationContext().getBean(
SpringSecurityHelper.SPRING_SECURITY_HELPER);
}

return securityHelper;
}

protected void setSecurityHelper(SpringSecurityHelper securityHelper) {
this.securityHelper = securityHelper;
}

/**
* {@inheritDoc}
*/
public void publishNotification(Notification notif) {
getSecurityHelper().switchUserIfUnauthenticated(getProxyUser());
getNotificationPublisher().sendNotification(notif);
}

/**
* Setter for notificationPublisher.
* @param notificationPublisher NotificationPublisher
*/
public void setNotificationPublisher(NotificationPublisher notificationPublisher) {
this.notificationPublisher = notificationPublisher;
}

/**
* Getter for the notificationPublisher.
* @return NotificationPublisher Returns the notificationPublisher.
*/
private NotificationPublisher getNotificationPublisher() {
return notificationPublisher;
}

/**
* {@inheritDoc}
*/
public String getProxyUser() {
return proxyUser;
}

/**
* {@inheritDoc}
*/
public void setProxyUser(String username) {
this.proxyUser = username;
}
}

最佳答案

将以下声明添加到我的 spring 上下文文件后,错误消失了

<property name="notificationListeners">
<list>
<bean class="com.xyz.mbean.impl.XYZNotificationListenerBean">
<constructor-arg>
<ref bean="notificationListenerToDatabase" />
</constructor-arg>
<property name="namingStrategy" ref="mbeanNamingStrategy" />
<property name="mappedObjectNameTypePairs">
<map>
<entry>
<key>
<value>sequenceGeneratorEarNotificationMBean</value>
</key>
<ref bean="NotificationMBean" />
</entry>
</map>
</property>
</bean>
<bean class="com.xyz.mbean.impl.XYZNotificationListenerBean">
<constructor-arg>
<ref bean="notificationListenerToTopic" />
</constructor-arg>
<property name="namingStrategy" ref="mbeanNamingStrategy" />
<property name="mappedObjectNameTypePairs">
<map>
<entry>
<key>
<value>sequenceGeneratorEarNotificationMBean</value>
</key>
<ref bean="NotificationMBean" />
</entry>
</map>
</property>
</bean>
<bean class="com.xyz.mbean.impl.XYZNotificationListenerBean">
<constructor-arg ref="notificationListenerToExternal" />
<property name="namingStrategy" ref="mbeanNamingStrategy" />
<property name="mappedObjectNameTypePairs">
<map>
<entry key="sequenceGeneratorEarNotificationMBean" value-ref="NotificationMBean" />
</map>
</property>
</bean>
</list>
</property>
</bean>

关于java - Spring 2.5.6 JMX NotificationPublisher 调用抛出 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39061510/

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