【问题标题】:Spring Bean never set as ManagedProperty in JSF BeanSpring Bean 从未在 JSF Bean 中设置为 ManagedProperty
【发布时间】:2014-08-23 04:39:45
【问题描述】:

注意:问题已经解决,目前我无法发布答案,只是发布以通知罕见情况。见底部评论。谢谢

我正在尝试将一个 spring bean 作为托管属性注入到 JSF bean 中。 初始化 JSF Bean 时不会抛出异常。 但 ManagedProperty 为空。我在 setter 方法上放了一些日志,但似乎从未调用过它。但我可以通过@Postconstruct 方法获取 Spring bean:

WebApplicationContext webAppContext = ContextLoader.getCurrentWebApplicationContext();
modelOperations = (ModelOperations) webAppContext.getBean("modelOperations");

PS:我知道有很多类似的帖子,花了一整天,但读完却筋疲力尽,最终不知道为什么它不起作用

环境:Spring 3.2.1,JSF:2.2.4,Eclipse 上的 Jetty

@ManagedBean(name = "batchOperation")
@SessionScoped
public class BatchOperation implements Serializable {

    @ManagedProperty(value = "#{modelOperations}", name = "modelOperations")
    ModelOperations modelOperations;

    @PostConstruct
    public void prepareLazyDataModel() {
        ..
        List<XXX> list = modelOperations.getXXXList()
        ..
    }

    public ModelOperations getModelOperations() {
        return modelOperations;
    }

    public void setModelOperations(ModelOperations modelOperations) {   
        this.modelOperations = modelOperations;
    }
}

ModelOperations bean:

package xxxxx.dao;

@Component(value = "modelOperations")
@Scope("prototype")
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, value = "transactionManager")
public class ModelOperationsImpl implements ModelOperations {

    protected SessionFactory sessionFactory;
}

faces.config:

<?xml version="1.0" encoding="utf-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                                  http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
              version="2.2">

<application>
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>

applicationContext.xml:

<context:annotation-config/>

<context:component-scan base-package="xxxxx"
    name-generator="xxxxx.util.WithoutTrailingImplBeanNameGenerator">
    <context:include-filter type="regex"
        expression="xxxxx.dao.*" />
</context:component-scan>

(如果有人想知道,请说) 没有TrailingImplBeanNameGenerator:

public class WithoutTrailingImplBeanNameGenerator extends
        AnnotationBeanNameGenerator {
    public String generateBeanName(BeanDefinition definition,
            BeanDefinitionRegistry registry) {
        String beanName = super.generateBeanName(definition, registry);
        if (!beanName.endsWith("Impl")) {
            return beanName;
        }
        return beanName.substring(0, beanName.length() - "Impl".length());
    }
}

【问题讨论】:

  • 我在发帖之前解决了这个问题。我只是在这里发布通知人们,因为我在互联网上没有看到类似的解释。这只是一个xsd版本问题。在我将 applicationContext 和 databaseContext xmls 上的 springs xsd 版本从 3.0 更新到 3.2 后,setter 被正常调用。
  • 谢谢!顺便说一句,欢迎来到 SO。

标签: spring jsf-2


【解决方案1】:

这只是一个xsd版本问题。在我将 applicationContext 和 databaseContext xmls 上的 springs xsd 版本从 3.0 更新到 3.2 后,setter 被正常调用

xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
            http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd
             http://jax-ws.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd">

【讨论】:

    猜你喜欢
    • 2011-05-15
    • 2015-05-01
    • 1970-01-01
    • 2011-07-07
    • 2012-08-27
    • 2012-02-14
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多