This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Is it possible to initialize a no-interface Stateful bean through annotation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Is it possible to initialize a no-interface Stateful bean through annotation?" Watch "Is it possible to initialize a no-interface Stateful bean through annotation?" New topic
Author

Is it possible to initialize a no-interface Stateful bean through annotation?

Rob Micah
Ranch Hand

Joined: Aug 30, 2011
Posts: 94
If I have a method defined in my Stateful bean I want to be an "initializer" can I use that somehow when creating a reference to it in a client through the @EJB annotation?
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

I'm not sure what you want to do :-)

Do you want just to have an initializer method for your SFSB? If so, can you use the @PostConstruct annotation on your method?

Cheers!


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Rob Micah
Ranch Hand

Joined: Aug 30, 2011
Posts: 94
I want to have a method in the bean like

So that when the bean is instantiated it's provided a String to work with. And restrict it to only being initialized that way. I just don't know how to make that work with something like this:
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

And where from would you like this String argument come? From the client?
Rob Micah
Ranch Hand

Joined: Aug 30, 2011
Posts: 94
Yes it will come from the client.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

As far as I know you can only initialize an EJB using @PostConstruct method which takes no parameters. It's invoked by the container and it's the only hook to use between class instantiatation and giving it's EJB nature.

I think you'll need to go with specific business method that you'll invoke in order to store the data as a part of the conversational state. If the particular data / parameter is required to be set for further processing, you can check this and throw i.e. some IllegalStateException or ApplicationException.
Rob Micah
Ranch Hand

Joined: Aug 30, 2011
Posts: 94
I thought there was a way to do this, without using annotation/dependency injection, by creating an ejbCreate() method in the local interface that had parameters and then when looking up through JNDI you pass those parameters to the create() method. Am I mistaken?
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Then sorry, I'm not aware of how the EJB 2.x were used... I worked only with 3.x.
Anastasia Klimchuk
Greenhorn

Joined: Sep 21, 2011
Posts: 10
Actually, there are several annotations that deal with Stateful beans initialization,

But this seems like returning to old style. As I understand, these annotations were mainly added to provide backward compartibility with EJB 2.1.

and... wow the same advice is here http://www.coderanch.com/t/318886/EJB-JEE/java/EJB-Passing-intialization-parameters-absence
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to initialize a no-interface Stateful bean through annotation?
 
Similar Threads
JPA - Scope extended and transaction
Implementing Interfaces in Bean - Required?
Can I override session bean type in the deployment descriptor?
How to get Stateful bean from SessionFacade
Distinction between Stateful and Standard session bean.