6

In order to use the @Autowire annotation, the object where you use the annotation must come from the spring context.

JSF managed beans are created by JSF's IOC not Springs, therefor i cannot use @Autowire inside of them must must use faces-config.xml and managed properties.

I already setup an EL resolver that lets be have spring beans as managed properties, i want to take it one step further and get rid of the need to go into the faces-config.xml every time i need to autowire something. Is this possible?

mkoryak
  • 57,086
  • 61
  • 201
  • 257
  • Possible duplicate of [Spring JSF integration: how to inject a Spring component/service in JSF managed bean?](https://stackoverflow.com/questions/18387993/spring-jsf-integration-how-to-inject-a-spring-component-service-in-jsf-managed) – ihebiheb Jul 30 '18 at 08:20

2 Answers2

12

Just annotate your managed beans with @Controller (or @Component), and @Scope("request") (or session) and add <context:component-scan> (if you haven't), and managed beans will automatically be detected as spring beans. And since you are already using the ELResolver, that should be it - you should be able to use @Autowired (or better - @Inject, if using spring 3.0).

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • are you saying that i should remove the managed beans from faces-config.xml completely? – mkoryak Dec 16 '10 at 16:15
  • 1
    @Bozho what about in JSF 2 the new View scope is really useful does spring comes with that scope for beans?? – Necronet Jan 25 '11 at 21:35
  • @Necronet - no, spring does not have this scope. It's a good question how to achieve such an integration between JSF and spring so that this scope is supported. I don't know the answer. – Bozho Jan 25 '11 at 21:41
  • @Bozho - i belive there is another way if using the org.springframework.web.jsf.SpringBeanVariableResolver you might be able to inject the bean through the faces-config – Necronet Jan 25 '11 at 21:49
  • well, using the resolver, you 'surrender' the managed bean facility of jsf to spring. So I don't think it would work. – Bozho Jan 25 '11 at 21:54
  • Quoting: "The recommended way to integrate Spring with JSF is to configure the Spring DelegatingVariableResolver in the faces-context.xml. The elements in a faces-config.xml file allows a Faces-based application to register a custom replacement class for the implementation of the standard Faces VariableResolver implementation. The Spring DelegatingVariableResolver first delegates to the original resolver of the underlying JSF implementation, then to the Spring root WebApplicationContext." – Necronet Jan 26 '11 at 20:34
  • quoted from http://weblogs.java.net/blog/caroljmcdonald/archive/2007/06/sample_applicat.html – Necronet Jan 26 '11 at 21:03
  • Yes, that's clear, but I still don't think it will work. (I've only used spring + jsf with an el-resolver, so I'm "on your side" on this) Here's why I think it won't work - a bean is either managed by JSF or by spring. If it is managed by JSF, it can be in view scope, but Autowired won't be resolved. If it is managed by spring, then Autowired will be resolved, but no view scope. – Bozho Jan 26 '11 at 21:21
  • why if you injected through faces-config.xml do you think that can be done?? i mean inject the spring bean into the jsf bean!! – Necronet Jan 26 '11 at 21:30
  • no, you can't. it has to be managed by spring in order to inject it. Well, you can, but you need some "hacking", and it will be ugly. For example, obtain the spring app context in the jsf bean, and call `appCtx.autowire(this)` - this will inject spring dependencies. – Bozho Jan 26 '11 at 21:32
  • seems weard cuz i am actually wiring my Spring Service Bean in the faces config..... right now!! – Necronet Jan 26 '11 at 23:29
  • You can ask a question, giving all details – Bozho Jan 27 '11 at 06:24
4

You can use @ManagedProperty(#{'someBean'}) for autowire other beans in jsf bean

E A
  • 995
  • 1
  • 10
  • 24