Skip to main content

@Inject + @ManagedBean vs @EJB

3 replies [Last post]
Anonymous

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Dominik Dorn

Hi,

I want my app to be portable with other frameworks (mainly spring 3.0) and
thought
it would be possible to replace the @EJB annotations with @Inject, but
nope.. that does not work..

whats wrong with lets say, this bean? :

@ManagedBean
@ApplicationScoped
public class MemberListService {

@Inject
private PersonDao personDao;

public MemberListService() {
}

public List findAll() {
return personDao.findAll();
}

public void persist(Person person) {
personDao.persist(person);
}

}

and the bean accessing the service

@ManagedBean
@RequestScoped
public class MemberListBean {
private Logger log = Logger.getLogger(MemberListBean.class);

private List memberList;

public List getMemberList() {
return memberList;
}

public void setMemberList(List memberList) {
this.memberList = memberList;
}

@Inject
private MemberListService service;

public MemberListService getService() {
return service;
}

public void setService(MemberListService service) {
this.service = service;
}

public MemberListBean() {

}

@PostConstruct
void loadData() {
System.out.println("calling postConstruct");

log.debug("calling postConstruct");
if (service != null) {
System.out.println("loading personDao.findAll()");
setMemberList(service.findAll());
System.out.println("loaded data");
}
else
{
log.warn("PersonDAO is null!");
}
log.debug("poluted memberList");
log.debug("memberlist now has " + memberList.size() + " items");
}

}

when using @Inject instead of @EJB, the loadData() method throws a NPE,
because service is null, althought it should be injected.

I have created an empty beans.xml in my /WEB-INF/ dir, as explained in "The
Java EE 6 Tutorial Volume 1"

What am I doing wrong?

Greetings,
Dominik
[att1.html]

Alexis Moussine-Pouchkine

Do you mean @javax.annotation.ManagedBean or JSF ?
If it's the latter you should use @Resource to inject another managed
bean or keep @EJB if it's an EJB.
If you want to use JSR 299 (CDI), you should use @Named instead of
@ManagedBean.
I'll admit I haven't looked into the that part of the tutorial yet,
you should look at the weld-servlet and weld-guess samples that you
can get straight from the GlassFish update center.

-Alexis

On Dec 16, 2009, at 23:28, Dominik Dorn wrote:

> Hi,
>
> I want my app to be portable with other frameworks (mainly spring
> 3.0) and thought
> it would be possible to replace the @EJB annotations with @Inject,
> but nope.. that does not work..
>
> whats wrong with lets say, this bean? :
>
> @ManagedBean
> @ApplicationScoped
> public class MemberListService {
>
> @Inject
> private PersonDao personDao;
>
> public MemberListService() {
> }
>
> public List findAll() {
> return personDao.findAll();
> }
>
> public void persist(Person person) {
> personDao.persist(person);
> }
>
>
> }
>
> and the bean accessing the service
>
> @ManagedBean
> @RequestScoped
> public class MemberListBean {
> private Logger log = Logger.getLogger(MemberListBean.class);
>
> private List memberList;
>
> public List getMemberList() {
> return memberList;
> }
>
> public void setMemberList(List memberList) {
> this.memberList = memberList;
> }
>
> @Inject
> private MemberListService service;
>
> public MemberListService getService() {
> return service;
> }
>
> public void setService(MemberListService service) {
> this.service = service;
> }
>
> public MemberListBean() {
>
> }
>
> @PostConstruct
> void loadData() {
> System.out.println("calling postConstruct");
>
> log.debug("calling postConstruct");
> if (service != null) {
> System.out.println("loading personDao.findAll()");
> setMemberList(service.findAll());
> System.out.println("loaded data");
> }
> else
> {
> log.warn("PersonDAO is null!");
> }
> log.debug("poluted memberList");
> log.debug("memberlist now has " + memberList.size() + "
> items");
> }
>
>
> }
>
>
> when using @Inject instead of @EJB, the loadData() method throws a
> NPE, because service is null, althought it should be injected.
>
> I have created an empty beans.xml in my /WEB-INF/ dir, as explained
> in "The Java EE 6 Tutorial Volume 1"
>
> What am I doing wrong?
>
> Greetings,
> Dominik
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

Paul Sandoz

On Dec 17, 2009, at 9:22 AM, Alexis Moussine-Pouchkine wrote:

> Do you mean @javax.annotation.ManagedBean or JSF ?
> If it's the latter you should use @Resource to inject another
> managed bean or keep @EJB if it's an EJB.
> If you want to use JSR 299 (CDI), you should use @Named instead of
> @ManagedBean.

IIUC @Named is a special qualifier associated with types of references
to be injected e.g.:

@Inject @Named("driver") Seat driverSeat;

@ManagedBean should be redundant if CDI is enabled, by including an
empty beans.xml in the WEB-INF directory. The application did not
produce a binding error at deployment for @Inject of
MemberListService, so i am guess that CDI might not have been enabled.

I cannot recall the details of how one can use @Inject to obtain a
reference to an EJB. Ken would definitely know.

> I'll admit I haven't looked into the that part of the tutorial yet,
> you should look at the weld-servlet and weld-guess samples that you
> can get straight from the GlassFish update center.
>
> -Alexis
>
> On Dec 16, 2009, at 23:28, Dominik Dorn wrote:
>
>> Hi,
>>
>> I want my app to be portable with other frameworks (mainly spring
>> 3.0) and thought

JSR 330, which defines @Inject, might get you some of the way but it
does not define a portable binding. Hence why JSR 299 is required
which also specifies scopes and bindings. The annotations
@ApplcationScoped and @RequestScoped are part of JSR 299 and not JSR
330.

Paul.
[att1.html]