1

It's possible to inject stateless session bean into jsf managed bean?

I have

@ManagedBean(name = "imageUpload")
@RequestScoped
public class ImageUploadBean extends FileUploadBean { 

    @EJB
    GenericEntityService genericEntityService;

    ...
}


public interface GenericEntityService {

    <T> T getById(int id, Class<T> entityClass);

    void deleteById(int id,  Class entityClass);
}


@Stateless
public class GenericEntityServiceImpl
        extends EntityServiceBase implements GenericEntityService
{
...
}

genericEntityService is always null, i have no idea why

i use tomee-webprofile-1.5.2

any help or link to example would be appreciated

Lostboy
  • 458
  • 3
  • 16
  • are you seeing any nullPointerException ? – Ashish Dec 16 '13 at 19:11
  • You could try to inject the bean through setting GenericEntityService as `@ManagedProperty(value="#{NameOfYourStatelessBean}")` instead of `@EJB` - this method allows injection of f.e. managed session beans into beans that have a more tighter focus like request-scoped. But I guess this requires to annotated your stateless class with `@ManagedBean(name="NameOfYourStatelessBean")`. At least I remember having had a similar problem in a [university course a few years back](https://github.com/RovoMe/University/tree/master/DST_2011_SS_Assignment_3/1_jsf/web/src/dst3/ejb/jfs) – Roman Vottner Dec 16 '13 at 19:13
  • @Ashish no, it's just not initialized by injecting mechanism – Lostboy Dec 16 '13 at 19:13
  • Moreover, injecting a session-scoped bean into a JSF request scoped bean is rather [a bad practice](http://stackoverflow.com/questions/8887140/jsf-request-scoped-bean-keeps-recreating-new-stateful-session-beans-on-every-req) as the session bean is not tied to the browsers session, but to the lifetime of the request-scoped backing bean (read BalusC's answer in the link). – Roman Vottner Dec 16 '13 at 19:31
  • Try adding a setter for your `GenericEntityService genericEntityService` field. – Luiggi Mendoza Dec 16 '13 at 19:32
  • @RomanVottner OP's problem is specific: how to inject a **stateless** EJB in a managed bean? – Luiggi Mendoza Dec 16 '13 at 19:37
  • It seems to be that more than one EJB implements the GenericEntityService interface, am I right? – Gabriel Aramburu Dec 20 '13 at 01:07

1 Answers1

0

Update: This answer is useful, becuase by default the interface is @Local

AFAIR It could be beacuse you need to specify the Local or Remote Annotation over your interface try it:

@Local
public interface GenericEntityService {

    <T> T getById(int id, Class<T> entityClass);

    void deleteById(int id,  Class entityClass);
} 
Cesar Loachamin
  • 2,740
  • 4
  • 25
  • 33