Spring Bean从未在JSF Bean中设置为ManagedProperty

时间:2014-07-02 11:44:18

标签: spring jsf-2

注意:问题已经解决,目前我无法发布答案,但刚发布以告知罕见情况。请参阅底部的评论。日Thnx

我正在尝试将一个spring bean注入JSF bean作为托管属性。 初始化JSF Bean时不会抛出异常。 但是ManagedProperty为null。我在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>

(如果有人想知道的话) WithoutTrailingImplBeanNameGenerator:

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());
    }
}

1 个答案:

答案 0 :(得分:0)

这只是一个xsd版本的问题。我在applicationContext和databaseContext xmls上将spring 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">
相关问题