【问题标题】:SystemEventListener + @Value null on Websphere 8.5Websphere 8.5 上的 SystemEventListener + @Value null
【发布时间】:2014-02-03 19:06:41
【问题描述】:

我在实现的一个 SystemEventListener 中遇到了问题。我正在使用注释 @Value 从属性文件中注入一个值。问题是注释 @Value 在应用程序的其他部分(例如 @Controller)中运行良好,但在此 SystemEventListener 中运行不佳。它也适用于 tomcat,但不适用于 Websphere 8.5。

你知道它可能是什么吗?

实施

public class PreRenderViewListener implements SystemEventListener {

    @Value("${path}/fileName.properties")
    private String filePath;

faces-config.xml

<system-event-listener>
            <system-event-listener-class>com.package.PreRenderViewListener</system-event-listener-class>
            <system-event-class>javax.faces.event.PreRenderViewEvent</system-event-class>
</system-event-listener>

解析器是一个扩展类PropertyPlaceholderConfigurer的bean,运行良好。

<bean id="uxMasterConfigBean" class="com.csc.cscip.ux.common.util.UXPropertiesConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <value>classpath:/resources/master/ux.default.configuration.properties</value>
                <value>${ux.master.configuration.file.path}</value>
            </list>
        </property>
    </bean>

此配置在Tomcat中运行良好,所以我认为这不是代码问题,可能是与Websphere 8.5相关的问题。我一直在互联网上寻找这个问题,但我没有找到类似的东西。

感谢您的帮助!

【问题讨论】:

  • 我会很惊讶它会起作用,基本上,WebSphere 中的行为是我期望发生的。 PreRenderViewListener 是 JSF(面孔)托管 bean,而不是 Spring 托管 bean,因此 @Value 不应该工作。

标签: java spring jakarta-ee websphere websphere-8


【解决方案1】:

正如 M. Deinum 所说,

PreRenderViewListener 是一个 JSF(面孔)托管 bean,而不是一个 spring 托管 bean,所以 @Value 不应该工作。

所以要让它工作,你必须让你的PreRenderViewListener被Spring“识别”,这意味着它必须是一个Spring组件。
我现在正在使用的一个解决方案(我希望得到关于它的评论)是使用multicaster,如org.springframework.web.jsf.DelegatingPhaseListenerMulticaster,它将在faces配置中注册,并将所有工作委托给其他实现@987654324的Spring bean @.
这是多播器的代码(它的灵感来自提到的 Spring 多播器)。

package org.example.listeners;
import java.util.Collection;

import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;

import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;


public class DelegatingSystemEventListenerMulticaster implements SystemEventListener{

@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
    for (SystemEventListener listener : getDelegates(FacesContext.getCurrentInstance())) {
        if(listener.isListenerForSource(event.getSource())){
            listener.processEvent(event);
        }
    }

}

@Override
public boolean isListenerForSource(Object source) {
    for (SystemEventListener listener : getDelegates(FacesContext.getCurrentInstance())) {
        if(listener.isListenerForSource(source)){
          return true;
        }
    }
    return false;
}


/**
 * Obtain the delegate PhaseListener beans from the Spring root WebApplicationContext.
 * @param facesContext the current JSF context
 * @return a Collection of PhaseListener objects
 * @see #getBeanFactory
 * @see org.springframework.beans.factory.ListableBeanFactory#getBeansOfType(Class)
 */
protected Collection<SystemEventListener> getDelegates(FacesContext facesContext) {
    ListableBeanFactory bf = getBeanFactory(facesContext);
    return BeanFactoryUtils.beansOfTypeIncludingAncestors(bf, SystemEventListener.class, true, false).values();
}

/**
 * Retrieve the Spring BeanFactory to delegate bean name resolution to.
 * <p>The default implementation delegates to {@code getWebApplicationContext}.
 * Can be overridden to provide an arbitrary ListableBeanFactory reference to
 * resolve against; usually, this will be a full Spring ApplicationContext.
 * @param facesContext the current JSF context
 * @return the Spring ListableBeanFactory (never {@code null})
 * @see #getWebApplicationContext
 */
protected ListableBeanFactory getBeanFactory(FacesContext facesContext) {
    return getWebApplicationContext(facesContext);
}

/**
 * Retrieve the web application context to delegate bean name resolution to.
 * <p>The default implementation delegates to FacesContextUtils.
 * @param facesContext the current JSF context
 * @return the Spring web application context (never {@code null})
 * @see FacesContextUtils#getRequiredWebApplicationContext
 */
protected WebApplicationContext getWebApplicationContext(FacesContext facesContext) {
    return FacesContextUtils.getRequiredWebApplicationContext(facesContext);
}

}

faces-config.xml:

<system-event-listener>
        <system-event-listener-class>org.example.listeners.DelegatingSystemEventListenerMulticaster</system-event-listener-class>
<!--Use any event you want here -->
        <system-event-class>javax.faces.event.PostAddToViewEvent</system-event-class>
    </system-event-listener> 

最后,创建您的 SystemEventListener bean,例如:

@Component
public class FooComponentListener  implements SystemEventListener{

@Autowired
private FooService fooService;

@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
    //Do your business
}

@Override
public boolean isListenerForSource(Object source) {
//Or any other condition
    return (source instanceof HtmlInputText || source instanceof HtmlOutputText || source instanceof HtmlOutputLabel);
}


}

我希望这会有所帮助,即使有点晚了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多