1

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

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: I know there are many similar threads, spent whole day, but exhausted reading them ending up with no clue why it is not working

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:

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>

(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());
    }
}
  • I solved the problem before posting here. I just posted here to inform people since I have not saw similar explanation on Internet. 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. – user3793571 Jul 02 '14 at 11:49
  • Thanks! Welcome to SO, by the way. – Sneftel Jul 02 '14 at 12:03

1 Answers1

0

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