This question already has answers here:
When is it necessary or convenient to use Spring or EJB3 or all of them together?
(2 answers)
Closed 3 years ago.
I need some clarification. I know how to work with JSF and its corresponding session beans, but i am getting confused with EJB. What is the difference between the beans introduced with EJB and the session beans used with JSF (for ejb i know about the stateless/full session beans and entity beans, entity manager, etc.). What i just dont get is when to use EJB and when to use jsf beans. Aside from the entity beans, both the ejb stateful/less session beans seem similar to the jsf session beans. I've read about injection ejb's into jsf, but why not just use ejb in conjunction with jsf beans? I hope you can understand my confusion. Thank you.
First of all, we need to know about the difference between JSF and EJB beans.
JSF beans are POJO classes which used to read the component value of JSF. There are two type of beans in JSF:
Managed bean is about how a java bean is created and initialized. As you know, JSF uses the Lazy initialization model. It means that the bean in the particular scope is created and initialized not at the moment when the scope is started, but on-demand, i.e. when the bean is first time required.
Backing bean is about the role a particular managed bean plays. This is a role to be a server-side representation of the components located on the page. Usually, the backing beans have a request scope, but it is not a restriction.
EJB Bean is a server-side component that encapsulates the business logic of an application. The business logic is the code that fulfills the purpose of the application.
Mainly, there are three types of session beans:
1.Statefull session bean
2.Stateless session bean
3.Singleton session bean(ejb 3.1)
There is indeed some confusion between the different types of managed beans in Java EE. To add to the confusion, Java EE 6 has introduced a third kind of managed bean: a CDI bean.
In this answer I try to explain the differences and similarities a little: How do CDI and EJB compare? interact?
Briefly said, JSF managed beans mainly don't offer support for transactions, which is something you often need when working with business logic and especially JPA.
Also note that the term session as in session scoped managed beans is a completely different kind of session than the one the term in stateless and statefull session beans refers to.
There is another gread answer on the site where the differences of CDI and EJB are explained. It helps a great deal when you finally grasp the whole picture. Where to use EJB 3.1 and CDI?
Related
I am working on a JSF web application. The service layer is developed using stateless session beans. These stateless beans are injected to managed beans using CDI.
I know that to manage transactions in stateless beans, I can use either container managed transactions or bean managed transactions. Also all the public methods in a stateless bean are by default in container managed transactions.
So my questions are:
Which is the preferred approach for transaction management in stateless bean - container managed or bean managed?
Is it advisable to have both bean managed and container managed transaction beans in service layer?
Is it possible to use both container managed and bean managed transactions in a single bean? If possible is it advisable?
Please let me know your suggestions...
Which is the preferred approach for transaction management in stateless bean - container >managed or bean managed?
The typical and preferred approach is to use CMT. Transaction management is one of the useful services that a app server offers, it simplifies your development, therefore, you should use this approach (that also is the default one) the vast majority of the time.
However, BMT is still necessary in some special cases:
a) when you need reduce the transaction boundaries for improving performance.
b) when you have a statefull session bean and you need retain a transaction across several client calls. (it's hard to see when this can be useful).
Is it advisable to have both bean managed and container managed transaction beans in service layer?
Yes, If some services need the special requirement described above, you can use both bean transaction types as part of the service layer.
Is it possible to use both container managed and bean managed transactions in a single bean? If possible is it advisable?
No, it is not possible.
Use container managed transaction if your transaction scope does not span across more service layer methods: ideally you should have one transaction (container-triggered commit) for one method. If this is not the case, a bean managed transaction should be more practical, letting the caller decide when commit or rollback.
I use SSH for a some while, and some friends ask me what is bean, and difference between session bean and entity bean, and difference between stateful session bean and stateless session bean, is those concept only exists in EJB(I also want to ask is EJB some relation with SSH), or they are general concept?
and what are they?
what i mean SSH is Spring Struts and Hibernate, actually i do not know they three has some relationship with EJB?
And i want to know is that bean is concept in the context of EJB? And when we talks about other framework like SSH, we never said bean?
what is bean
In context of EJB, bean is a class managed by the container.
between session bean and entity bean
Session beans represent logic while entity beans represented persistent objects. These days entity beans aren't used anymore in favour to JPA entities.
difference between stateful session bean and stateless session bean
Once you obtain a reference to stateful session bean, you will always use that particular instance. Stateless session beans are pooled and returned to the clients at random.
those concept only exists in EJB
Yes, although beans are also present in Spring framework with a similar meaning but different design concepts.
is EJB some relation with SSH
You can deploy EJBs via SSH using SCP. But seriously, seems like you are confusing SSH with...?
Coming from plain old DI of Spring I can't figure out how to choose scopes properly while writing with CDI.
In Spring all my services have singleton scope by default, which I suppose maps to application scope in CDI (or even #Singleton). I know for e.g. logged in user information I need to use Session scope and for e.g. form params I need request scope.
Say I have a bean that hides external service API calls. It is completely stateless. Should I put it as #Singleton or simply application scoped? Or let it be created on every request (possibly bad option).
Is this correct to inject everything everywhere? In Spring I create my data objects by new. Should I do the same in CDI or simply #Inject them?
Are you only using CDI? or a Java EE 6 container? If you have a stateless class that is used for service calls, then I would recommend using #Stateless, which is from the EJB spec (so you would need a Java EE 6 container) It isn't a singleton, but it doesn't get created on each request either. I believe it is more closely bound to the session, but since it is stateless, instances can be pooled and shared. If you are only dealing with CDI, I believe Singleton matches more directly to Spring's singleton, but I would recommend using ApplicationScoped because it provides a proxy which makes serialization of beans that use it easier.
#Service
#Scope("prototype")
public class CustomerService
{
......
}
Just add #Scope("prototype") annotation to your component.
Is there a reason you would need the bean to remember it's state? If you are using something like a web client, that is a better place to store state in say, session scoped managed beans (assuming jsf), or whatever equivalent for your case. On the back end server side your EJB's would be better kept as #stateless to keep overhead to a minimum and help with the 'keep it simple s..." paradigm. And in case this works, just declare #Stateless on your bean. Unless there is a reason to use a singleton, again it is better to use a stateless bean if you want to go with a Java EE container for your services.
Stateless beans are not really being recreated with every request. That is the purpose of the pool. The app server keeps a ready supply of stateless beans on hand, and if it gets busy, it will make more, and if it quiets down, it will empty some out.
I'm working on a Java webapp trying to combine the following technologies:
Java EE 6
CDI
JSF 2
EJB 3.1
Spring Security
I provide CDI-based backing beans (#ViewScoped, #Named) for my JSF pages.
I use #Stateless EJB beans for the actual work to be done.
I only need few session information like jSessionCookie (managed by container), internal username and some other internal IDs. Now, I wonder where to put this session information so that I can access it in the backing beans for JSF, but also provide it to the stateless EJBs? Should I use a #Stateful EJB session bean or should I create CDI-based POJO with #SessionScoped and #Named?
Are there any best practices?
For your particular use case, a stateful session bean would not be a good choice.
Do note that contrary to what people may claim, stateful session beans are surely not something you should generally avoid. However, they are for advanced use cases, for instance when dealing with JPA's extended persistence context.
The reason why stateful session beans would not work here, is that they are not automatically associated with the HTTP session, which seems to be your prime concern. You could add the #SessionScoped annotation to them, but then you could just as well use a regular managed bean. You would not use any of the particular features of a SFSB.
See alo:
Can I use EJB Stateless Bean with CDI to maintain user session?
Using a Stateful Session Bean to track an user's session
Calling a CDI session scoped producer method from an EJB stateless session bean
Contexts and Dependency Injection in Java EE 6
You can inject your stateless EJBs with a session scoped CDI bean, but you have to realize that within the same application your EJB bean would be dependent on the HTTP session then (something you sometimes want to avoid, e.g. if your bean has to be called from other contexts as well).
#Stateful EJB is something I'd try to stay away from. I believe behavior and state are things that should not be mixed.
I would also go for SJuan76's answer and use SessionScoped JSF backing bean.
Why is Java EE 6 CDI missing the #ViewScoped and #FlashScoped annotations? (especially the former makes me wonder, because CDI stems from the Seam world, which already knew the very similar ScopeType.PAGE...)
What are the recommended workarounds when using CDI? Use Seam 3?
Thanks
The #ViewScoped is specific to the MVC framework JSF, not to the dependency injection framework CDI. The view scope lives as long as you're interacting with the same JSF view. CDI has not really a notion of "views". The CDI alternative to that is #ConversationScoped which lives longer than the request scope, but shorter than the session scope. You only have to control the termination yourself. You can if necessary use MyFaces CODI to bridge the JSF #ViewScoped to CDI #Named beans. The upcoming JSF 2.2 will have a CDI compatible #ViewScoped in the javax.faces.view package.
The #FlashScoped doesn't exist in JSF. The JSF flash scope exist of basically a map which is backed by a short-living cookie which survives HTTP redirects. You cannot let JSF put managed beans in this scope. You've to manually put/get the values in/from the map yourself and/or use the #{flash} reference in EL which basically refrences the map. Seam Faces has however hijacked the JSF specific javax.faces.bean package for its #FlashScoped annotation, but this is definitely not from standard JSF API.
See also:
Add items to List in Request Scoped Bean - contains some concrete examples of CDI alternatives
You can implement the context and use the #NormalScope to create your own CDI Scope witout using any other framework or waiting for the new JEE7
CDI fires an event AfterBeanDiscovery after each bean call
You can use CDI extension to #Observes this event and add your context implementation
In your scope implementation you can :
Use Contextual to get your bean by its name from FacesContext ViewRoot Map and return it after each ajax call back
Use CreationalContext if the bean name from first step is not found to create it in the FacesContext ViewRoot Map
for a more in-depth explanation i recommand this link : http://www.verborgh.be/articles/2010/01/06/porting-the-viewscoped-jsf-annotation-to-cdi/