Is JSF managedBeans and JMX managedBeans the same? - jsf

Maybe a little bit stupid question, but is JSF managedBeans and JMX managedBeans the same? Can I access, for example, JSF managedBean through JMX?

No, JMX beans and JSF beans are completely different concepts. JSF beans are primarily accessible via EL expressions and JMX beans can be accessed using the standard Java management interfaces.

Related

HttpSession Attribute vs SessionScoped CDI Bean [duplicate]

I'm a little confused by the mixed use of JSF2+Spring+EJB3 or any combination of those. I know one of the Spring principal characteristics is dependency injection, but with JSF managed beans I can use #ManagedBean and #ManagedProperty anotations and I get dependency injection functionality. With EJB3 I'm even more confused about when to use it along with JSF or if there is even a reason to use it.
So, in what kind of situation would it be a good idea to use Spring+JSF2 or EJB3+JSF2?
Until now I have created just some small web applications using only JSF2 and never needed to use Spring or EJB3. However, I'm seeing in a lot of places that people are working with all this stuff together.
First of all, Spring and EJB(+JTA) are competing technologies and usually not to be used together in the same application. Choose the one or the other. Spring or EJB(+JTA). I won't tell you which to choose, I will only tell you a bit of history and the facts so that you can easier make the decision.
Main problem they're trying to solve is providing a business service layer API with automatic transaction management. Imagine that you need to fire multiple SQL queries to perform a single business task (e.g. placing an order), and one of them failed, then you would of course like that everything is rolled back, so that the DB is kept in the same state as it was before, as if completely nothing happened. If you didn't make use of transactions, then the DB would be left in an invalid state because the first bunch of the queries actually succeeded.
If you're familiar with basic JDBC, then you should know that this can be achieved by turning off autocommit on the connection, then firing those queries in sequence, then performing commit() in the very same try in whose catch (SQLException) a rollback() is performed. This is however quite tedious to implement everytime.
With Spring and EJB(+JTA), a single (stateless) business service method call counts by default transparently as a single full transaction. This way you don't need to worry about transaction management at all. You do not need to manually create EntityManagerFactory, nor explicitly call em.getTransaction().begin() and such as you would do when you're tight-coupling business service logic into a JSF backing bean class and/or are using RESOURCE_LOCAL instead of JTA in JPA. You could for example have just the following EJB class utilizing JPA:
#Stateless
public class OrderService {
#PersistenceContext
private EntityManager em;
#EJB
private ProductService productService;
public void placeOrder(Order newOrder) {
for (Product orderedproduct : newOrder.getProducts()) {
productService.updateQuantity(orderedproduct);
}
em.persist(newOrder);
}
}
If you have a #EJB private OrderService orderService; in your JSF backing bean and invoke the orderService.placeOrder(newOrder); in the action method, then a single full transaction will be performed. If for example one of the updateQuantity() calls or the persist() call failed with an exception, then it will rollback any so far executed updateQuantity() calls, and leave the DB in a clean and crisp state. Of course, you could catch that exception in your JSF backing bean and display a faces message or so.
Noted should be that "Spring" is a quite large framework which not only competes EJB, but also CDI and JPA. Previously, during the dark J2EE ages, when EJB 2.x was extremely terrible to implement (the above EJB 3.x OrderService example would in EJB 2.x require at least 5 times more code and some XML code). Spring offered a much better alternative which required less Java code (but still many XML code). J2EE/EJB2 learned the lessons from Spring and came with Java EE 5 which offers new EJB3 API which is even more slick than Spring and required no XML at all.
Spring also offers IoC/DI (inversion of control; dependency injection) out the box. This was during the J2EE era configured by XML which can go quite overboard. Nowadays Spring also uses annotations, but still some XML is required. Since Java EE 6, after having learned the lessons from Spring, CDI is offered out the box to provide the same DI functionality, but then without any need for XML. With Spring DI #Component/#Autowired and CDI #Named/#Inject you can achieve the same as JSF does with #ManagedBean/#ManagedProperty, but Spring DI and CDI offers many more advantages around it: you can for example write interceptors to pre-process or post-process managed bean creation/destroy or a managed bean method call, you can create custom scopes, producers and consumers, you can inject an instance of narrower scope in an instance of broader scope, etc.
Spring also offers MVC which essentially competes JSF. It makes no sense to mix JSF with Spring MVC. Further Spring also offers Data which is essentially an extra abstraction layer over JPA, further minimizing DAO boilerplate (but which essentially doesn't represent the business service layer as whole).
See also:
What exactly is Java EE?
JSF Controller, Service and DAO
#Stateless beans versus #Stateful beans
There's no real easy answer here as Spring is many things.
On a really high level, Spring competes with Java EE, meaning you would use either one of them as a full stack framework.
On a finer grained level, the Spring IoC container and Spring Beans compete with the combination of CDI & EJB in Java EE.
As for the web layer, Spring MVC competes with JSF. Some Spring xyzTemplate competes with the JPA interfaces (both can use eg Hibernate as the implementation of those).
It's possible to mix and match; eg use CDI & EJB beans with Spring MVC, OR use Spring Beans with JSF.
You will normally not use 2 directly competing techs together. Spring beans + CDI + EJB in the same app, or Spring MVC + JSF is silly.

Purpose of CDI technology in Java EE and its relation to UI technologies like JSF and back end technologies like EJB

This question directly follows from another question of mine here. The last paragraph of the answer to that question mentions CDI technology.
Can you explain what is the purpose of that technology. Is there any relation of that technology with JSF and EJB? I have been through this question but I'm not clear on what is the purpose of that technology yet. I specifically want to know where it fits with other Java EE technologies like EJB and JSF.
Adding to the correct answer of Bozho:
CDI is a superset of JSF's managed beans, and JSF will eventually deprecate their own native managed bean system. In JSF 2.2 steps have already been taken for this.
CDI is not a superset of EJB beans, but they complement each other. CDI does not only provide more advanced DI to EJB beans, but is also capable of giving EJB beans a scope (mostly used for stateful beans). EJB on its turn provides transactional and security services among others , which CDI does not offer. Like JSF managed beans, EJB beans will eventually be merged into the CDI component model. Marina Vatkina (EJB spec lead) among others has strongly hinted at this.
Further reading:
How do CDI and EJB compare? interact?
CDI, when to break out the EJBs
First, CDI is a dependency-injection standard framework. It defines ways for objects to obtain their dependencies not through instantiation (private FooDao dao = new FooDao()) but via a container which creates and manages instances. You can read more about dependency injection (google/wiki).
The standard defines how that works with both JSF and EJB.
your JSF managed beans can be defined via CDI, so that you can inject services into the managed beans, and so that CDI manages the lifecycle of JSF managed beans
EJB can also benefit from the advanced dependency injection of CDI (previously they had their own, feature-poor DI).
In short, CDI binds all components in JavaEE in a way spring does with all of its components, but CDI is a standard.

Conversation Scope in JSF 2.0

I am trying to implement conversation scope in backing bean of JSF for our application. At first, I am trying to use MyFaces Orchestra. It seems to work well. However, I am concerning the case when the application deployed in cluster environment and during session replication. Then, I am thinking if MyFaces CODI and OpenWebBeans are better choice or not.
I have two questions to ask:
(1) MyFaces Orchestra claims that it does not support distributed sessions. Does it means that it does not work well in cluster environment?
(2) Does MyFaces CODI have any issues when deployed in cluster environment? I cannot found any information about this in its document.
If your server supports clustering of CDI session scoped beans, you can also cluster MyFaces CODI conversations.

Using the Seam framework without Stateful Session Beans?

Has anyone had success building a SEAM Application without using Stateful session beans? There is some confusion on a new project in which I think several stakeholders have essentially 'banned' stateful session beans... but some development is being done in SEAM.
Most of the literature on SEAM encourages the use of Stateful Session Beans. Thoughts?
You can absolutely use Seam without stateful session beans. You don't need any type of EJB at all, if you don't want them. Seam can be deployed on a variety of app servers, including Tomcat which doesn't support the use of EJBs. Seam has the ability to mimic a lot of the functionality that EJBs provide -- session scope, transactions, etc. -- without actually using an EJB.
Using Seam with Tomcat, for example, you can have a very robust application without EJBs that is lightweight, but acts in a manner similar to an application deployed on JBoss or Websphere that does make use of EJBs.

JSF 2.0 (Facelets) and Struts

I am a newbie with Java EE 6. I started with JSF 2.0, Facelets, Managed Beans and really liked it. It was a great switch from JSP and servlets. It also made me a confused person. It may be the lack of my knowledge, but I have many questions swirling in my mind.
Are Facelets and Managed beans a direct replacement of JSP and Servlets. (The job requirement here still focuses on JSP and Servlets, will the market/global trend move towards Facelets or the demand of JSP will remain)
What about integration of Struts 2.0, Hibernate and Spring with Java EE 6. I have seen lots of tutorials and working examples of these technologies with JSP, but havent them with Facelets.
My personal view:
JSF is built on top of JSP/Servlet -- it's not really a replacement, but a higher level abstraction.
Ideas in Hibernate and Spring have somehow been integrated under JPA and CDI.
Basic knowledge of JSP/Servlet is still usefull, what's not needed is knowledge of JSTL.
That said, there are two broad categories of web framework: component-based, and action-based. JSF is component based and each component is responsible of its own rendering and callbacks. Struts is action-based, where the controller forward to a view explicitly. Both can be seen as MVC, but the approach differ largely between the two. There are other frameworks of both types around (wicket, play, etc.)
Which kind of approach is best is subject to taste and opinion.
Struts 1 was a framework that added additional structure above a JSP/servlet application. I would develop with Struts in preference to raw JSP, and in fact many developers using JSP before Struts emerged evolved their own equivalent frameworks.
JSF is in effect a standardised equivalent of Struts 1, whose development was heavily influenced by the originators of Struts. So JSF is more or less a direct replacement of Struts, that in principle should be widely supported by Java EE App Server vendors. The adoption of JSF is not uniform, there are enthusiastic users, and otehr folks who stick with Struts 1. I have no statistics to tell you which technolgies are more widely used.
Both Struts and JSF and Model/View/Controller frameworks, and neither address how to implement the Model - in particular they don't say much about persistence, hence you need something in addition to Struts/JSF which is where Hibernate and JPA come in.
To my perception the emerging trend is to build UIs using Dojo or JQuery, with REST services, It appears that Struts 2 is attempting to address this problem domain, with rich widgets in the browser. This is interesting and worth study. Personally I've not used Struts 2, nor have I encountered it professionally, probably because I'm working with products that major on Dojo and iWidgets. I'm planning to give Struts 2 some attention.

Resources