@Inject annotation works just fine in a @ManagedBean. ( as long as you have a beans.xml in classpath ) Is there any harm in this?
Asked
Active
Viewed 139 times
1 Answers
2
@Inject
is a Java CDI annotation. There's no problem in using it while you have the proper context (you need a CDI container context prepared by yourself or a JavaEE application server).
If you migrate your application to a servlet container like Tomcat, you need to manage the context by yourself (as it is not considered a JavaEE AS) or you could replace the annotation by @ManagedBean
, but you'll be able only to inject JSF managed beans and not every single Java Bean.
-
In some answers I have seen if you are using Inject you should be using the Named annotation to make it a CDI bean? – Koray Tugay Sep 03 '13 at 06:41
-
Of course, I haven't done a lot of CDI, but as far as I know using `@Named` declares it as a context bean and you need it to be in the container to use `@Inject`. However you should check a CDI tutorial for that. – Aritz Sep 03 '13 at 06:43
-
Isn't it already answered in [other of your questions](http://stackoverflow.com/a/18579994/1199132)? I don't do CDI, I only have tried it in couple of projects. The answer I gave about `@Inject` is correct, but I can't go more deeply. – Aritz Sep 03 '13 at 06:54
-
While @XtremeBiker is correct, you may seem some oddities in your project. You'll essentially have two instances of the bean depending on the context. If you're using a bean in a backend service that is also a CDI bean you'll have the CDI bean with the lifecycle and other state. If you use it in the JSF page you'll get the JSF managed bean, which probably won't have the correct state as the other instance. – LightGuard Sep 03 '13 at 19:33