We have a very old and large project based upon Spring. We later added JSF and that's working now. We are now testing using CDI (and eventually omnifaces) to avoid the memory problems with viewscoped beans listed here: OmniFaces 3.0 and MyFaces 2.2.12 throws java.lang.NoSuchMethodError: javax.faces.application.ApplicationFactory
I see in this post Spring JSF integration: how to manage a bean and inject a Spring component/service? that seems to say we can leverage are Spring beans in a CDI bean. And it does kinda work, but it creates a new instance of the Spring bean and hoping to find a way to use the Spring managed copy that has been initialized.
That link was for JSF 2.2. I'm still on JSF 2.1. We are first testing to see if we can get CDI to work at all, then we'll upgrade to 2.2 and add omnifaces. But we need to prove out CDI can work with Spring first.
More details:
Spring Bean XML config.xml:
<bean id="configConstant"
class="com.me.service.ConfigConstant" scope="singleton">
<property name="rowsPerPage" value="${config.rowsperpage}" />
</bean>
in the config.properties we have:
config.rowsperpage=100
The default in the Class is 50. My faces-config.xml has this in it:
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
And this works great for a ton of JSF beans all defined in the faces-config.xml. I am testing one new bean to be CDI and we use wildfly 20.0.1. My test bean is:
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
@Named
@ViewScoped
public class TestCDI implements Serializable {
@Inject @ManagedProperty("#{configConstant}")
private ConfigConstant configConstant;
public TestCDI() {
super();
}
public ConfigConstant getConfigConstant() {
return configConstant;
}
public void setConfigConstant(ConfigConstant configConstant) {
this.configConstant = configConstant;
}
}
My testcdi.xhtml is like this:
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
template="common.xhtml">
<ui:define name="content">
<h:form>
<h:outputText value="#{testCDI.configConstant.rowsPerPage" />
</h:form>
</ui:define>
</ui:composition>
This does inject the configConstant bean but uses the default value of 50. In my JSF beans configured via faces-config.xml, it reports the correct value of 100. So I'm assuming that on page load we are creating a new instance of ConfigConstant and not using the instance that was created upon start and has the properties added.
My ConfigConstant is just a plain class, no annotations since it's being configured in the spring XML file.
public class ConfigConstant {
private Integer rowsPerPage = 50;
public Integer getRowsPerPage() {
return rowsPerPage;
}
public void setRowsPerPage(Integer rowsPerPage) {
this.rowsPerPage = rowsPerPage;
}
}
Given all of that, any idea how to get the version of ConfigConstant that has been initialized by Spring?
Thank you!