2

I have this bean:

@Component
@RequestScoped
public class Hello {

   @AutoWire
   public DBDao myDao;

   //Getters / Setters
}

When I add the @Component the class is now managed by the Spring container so it could inject my DAO for me. My question is, when I use this bean inside my JSF file like ${hello.myDao}, is it Spring or JSF that is providing me the bean?

If Spring is providing the bean, then how does the JSF container know about this bean when it is being managed by the Spring container?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
binary_assemble
  • 394
  • 3
  • 17

2 Answers2

1

You seem to be mixing JSF with EL. Those things ${} and #{} is EL. EL is not part of JSF. EL is a standalone API which happens to be used by both JSF and Spring. JSF and Spring are both capable of placing managed bean instances in EL scope in such way that EL can find and invoke it.

When you do #{foo}, then EL will pass literal "foo" through a chain of registered EL resolvers. If one of them can find the object instance associated with literal "foo", then it would be returned. There are default resolvers which look for the bean instance associated with request/session/application attribute name e.g. request.getAttribute("foo"). JSF makes use of those builtin resolvers. JSF basically does request.setAttribute("foo") under the covers so that EL can find it.

Spring does it a bit differently (like Java EE's own CDI) and therefore needs a custom EL resolver which has basically a register of all registered Spring beans. When Spring's EL resolver is hit, then Spring will look for the bean associated with the literal "foo" in its own register which is filled during webapp's startup based on @Component. If Spring can't find anything in its register matching "foo", then it will just pass through to the next EL resolver in the chain.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Amazing answer as always @BalusC. This really help answered my asked question AND unasked questions that I didn't know how to ask. Thank you! – binary_assemble Sep 11 '14 at 00:46
0

You need to tell FacesServlet to look for Spring dependencies as well by adding

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

to your faces-config.xml

assuming your Spring context has been loaded at runtime properly, if not you need to add listener to web.xml to initialize at startup time

jmj
  • 237,923
  • 42
  • 401
  • 438
  • I have all of the config file setup and everything works. I just want to know which container is managing my `Hello` bean. – binary_assemble Sep 04 '14 at 03:00
  • 1
    `Spring` if both of them have the same bean name it would be interesting to try out which one takes precedence or it conflicts – jmj Sep 04 '14 at 03:02