Spring Bean从未在JSF Bean中设置为ManagedProperty [英] Spring Bean never set as ManagedProperty in JSF Bean

查看:60
本文介绍了Spring Bean从未在JSF Bean中设置为ManagedProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:问题已经解决,目前我无法发布答案,而只是在极少数情况下发布通知.请参阅底部的评论.thnx

Notice: Problem already solved, currently I can't post the answer but just posted to inform for a rare case. See comment at bottom. Thnx

我试图将spring bean注入JSF bean作为托管属性.初始化JSF Bean时不会引发异常.但是ManagedProperty为空.我在setter方法上放了一些日志,但似乎从未调用过.但是我可以通过@Postconstruct方法获取Spring bean:

I am trying to inject a spring bean into JSF bean as managed property. No exception is thrown when JSF Bean is initialized. But ManagedProperty is null. I put some logs on setter method, but it seems it is never called. But I can get the Spring bean via at @Postconstruct method:

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

PS:我知道有很多类似的线程,整天都用完了,但是精疲力竭地阅读它们却最终不知道为什么它不起作用

PS: I know there are many similar threads, spent whole day, but exhausted reading them ending up with no clue why it is not working

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

Environment: Spring 3.2.1, JSF: 2.2.4, Jetty on Eclipse

@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:

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:

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:

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:

(Put that if anybody wonders) 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());
    }
}

推荐答案

这只是xsd版本的问题.在我将applicationContext和databaseContext xmls的xsd版本从3.0更新到3.2之后,通常会调用setter.

It was just an xsd version problem. The setter's was called normally after I updated springs xsd versions from 3.0 to 3.2 on applicationContext and databaseContext xmls

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">

这篇关于Spring Bean从未在JSF Bean中设置为ManagedProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆