2

Problem Description: My injected Spring bean defined as a Managed-Property to a JSF backing bean is not being instantiated. Its always coming up null when I retreive the Managed-Bean.

I have been fighting with this all day and it seems that JSF Managed Bean just won't read out of the applicationContext from Spring. I can manually pull out the bean by using the FacesContext in a backing bean and it finds the bean but when I try and inject it through the FacesConfig it always comes out null. I included my steps below how I integrated it. Any suggestions?

Configuration

Icefaces 1.85

JSF 1.2 (through ice faces servlet)

Spring 3.0

Websphere 7.5 ( Which is eclipse 3.5 I think )

Web.xml Configuration Changes

Spring Context Loader Listener

    <listener>
    <display-name>SpringListener</display-name>
    <icon>
        <small-icon>small.gif</small-icon>
        <large-icon>large.gif</large-icon>
    </icon>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Context Config

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/SpringConfig/SpringHelloWorld.xml
    </param-value> 
</context-param>

FacesContext Changes

Variable Resolver - AKA The Glue

<application><variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver></application>

Managed Bean

   <managed-bean>
    <managed-bean-name>testData</managed-bean-name>
    <managed-bean-class>src.test.TestData</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
        <property-name>spring3HelloWorldBean</property-name>
        <value>#{spring3HelloWorldBean}</value>
    </managed-property>
</managed-bean>

Spring.xml Config

<bean id="spring3HelloWorldBean" class="src.test.Spring3HelloWorld" />

Thanks in advance

haju
  • 1,278
  • 4
  • 20
  • 38

2 Answers2

12

First of all and I think this your problem that DelegatingVariableResolver is deprecated in all JSF version after 1.1 and you are using 1.2 so please use this following configuration.

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

Hope it helps.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
danny.lesnik
  • 18,479
  • 29
  • 135
  • 200
  • Thanks I will give that a try tomorrow first thing. I did try using a el-resolver but I don't think it was that class. I'll update tomorrow. Cheers for the help. – haju Jun 01 '11 at 01:33
  • @Bozho and @danny.lesnik it didn't seem to work. A key piece information I left out is that I am also using Icefaces 1.8 which uses jsf 1.2 . Not sure if that makes a difference. When using the the contextLoader Listener, it doesn't need any kind of "load" priority like you would set with the contextLoadServlet? – haju Jun 01 '11 at 12:18
  • @haju - did you try my suggestion with annotations? Does it work? – Bozho Jun 01 '11 at 12:19
  • @Bozho what spring jar has the Inject annotation? I added all the spring jars onto the build path and it can't seem to find it. I don't have to use the Controller annotation in order to use the inject one correct? I can still define a jsf managed bean and within that class for the bean use the inject annotation for spring beans right? – haju Jun 01 '11 at 13:04
  • it's jsr-330, but you can use `@Autowired` instead - it's the same – Bozho Jun 01 '11 at 13:35
  • @Bozho ya I tried it but didn't seem to inject it. As seen below where spring3HelloWorldBean is the name of the bean id in the spring.xml @Autowired Spring3HelloWorld spring3HelloWorldBean; – haju Jun 01 '11 at 14:05
  • @danny.lesnik and @Bozho I fixed the original problem of the injecting not working. I had a blasted typo above in my managed property. I had the class defined as the value. – haju Jun 01 '11 at 14:25
  • I like the approach though of the @Injected. It seems to be that is the same thing as using a JSF managed property for a Spring bean except you bypass the facescontext configuration and less chance of typo's. – haju Jun 01 '11 at 14:28
4

In addition to using SpringBeanFacesELResolver you can benefit even more from spring by using annotations. So instead of defining your managed bean in xml, just do:

@Controller
public class FooBean {
    @Inject
    private SpringService service;
    ....
}
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Does that mean I wouldn't have a need to define the bean at all in the facescontext? Would the same @Inject also apply to injecting another managed bean? I assume so. – haju Jun 01 '11 at 01:34
  • @haju - yes. All managed beans would live in the spring context. (You'll just have to include their package in the `component-scan`) – Bozho Jun 01 '11 at 05:59