I asked some IT students at my workplace to describe the principle of Information Hiding in Java.
CDI – Overview
CDI is an abbreviation of “Context and dependency injection” and it is defined in the JSR 299. I this post, I don’t introduce CDI in detail. For that I refere to well sources as en.wikipedia, JCP, DZone Ref-Card and also to good article in TheServerSide.com. I will explaine that things, that the most students grappled with it.
CDI isn’t activated out-of-the box in a Java EE 6 application. It is necessary to put a beans.xml deployment description file inside your Project (“…srcconf” – folder). You need it inside a Web Application(to use it with JSF/JSP/etc) as well as in an EJB-Module(if it exists, too). The file can be “empty” as follow:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> </beans>
Why should I use CDI
It is the easiest way to get handy with a Java EE 6 Application. CDI unify all of the various types of beans to “Managed Beans”. So forget “Backing Bean”, “Entity Bean”, “JSF-Managed Bean”, etc. Every POJO which is marked as a ManagedBean is managed by the Java EE Container.
What is the benefit of CDI
- You can simply inject every POJO inside your application
- The Context of the first call define the life cycle of all following bean calls (in the most case, it starts with a defined scope)
- A ManagedBean can simply extended with enterprise-services with an EJB-Annotation (as @javax.ejb.Stateless, @javax.ejb.Stateful, @javax.ejb.Singleton)
Here are more information about ManagedBean and the “extension” EJB.
Which annotation do I need to make a ManagedBean?
In short: Use @javax.annotation.Named
In detail: When you use @javax.annotation.Named on the top of your class, you get implicit the @javax.annotation.ManagedBean.
What is the different between the annotation: @javax.faces.bean.ManagedBean(name=”myBean”); @javax.annotation.ManagedBean(“myBean”) and @javax.inject.Named(“myBean”)?
In short: there is no different, use @javax.inject.Named
In detail: @javax.faces.bean.ManagedBean is refer to JSR314 and was introduced with JSF 2.0. The main goal was to avoid the configuration in the faces-config.xml file to use the bean inside an JSF Page. The annotation @javax.annotation.ManagedBean(“myBean”) and @javax.inject.Named(“myBean”) are almost the same, but the Named is shorter
. See here
What is the different between the Annotation @SessionScoped and @Stateful
In short: They are similar in some ways. Both don’t lose their states after a single call and both are ManagedBeans, but you use it on different location inside your application.
In detail: The Scoped-Annotations (@RequestScoped, @SessionScoped, etc.) are used in the client /web tier. Beans with a EJB-Annotation as like @Stateful or @Stateless (or @Singelton – see type of EJB’s) are located in the business tier. So the user interacts with a scoped annotated bean, where the Scoped annotated bean it self interacts with an EJB annotated Beans (of corse they can also interact with an other normal ManagedBean).
Keep in mind, that a Stateful Bean is only Stateful inside the callers context. I made a diagram, that should explain this behavior. Look at the Object Reference of each call (Ref: …). As I described here, not every combination make sense, i.e. a RequestScoped ManagedBean inject a Stateful ManagedBean.
When should I use the Annotation @EJB or the @Inject and when should I instance (new Class()) an object by my self
In short: Use @Inject
In detail:@EJB and @Inject are almost the same. In the most case you can use both, but
- @Inject is like generic and takes only the jndi-Name. In the most case you can use that
- @EJB can inject local and remote Beans. You need it, inside a Client Application which call an EJB inside a Java EE Container (remote call)
- new Class() is used for business functions. When you instance an object by your self, it isn’t managed by the container and you can’t use container managed functions (i.e. inject an other bean or a resource as a JMS Queue
A retrospect at half-year of Enterprise Application
A half year ago, I started with the final lab exercise for the Enterprise Application lecture at the HSLU (Modul Overview). My goal was to improve the documentation for the reference application, the interfaces to use and some technology hints. After that, I hoped, while the course, I have a cushy job.
But the last half year, I was surprised, frustrated and challenged by the students. In this first post, I won’t write about technical issues the students run into. Lot of students had more basic problems, where I wouldn’t have believed.
Environment: Know what you have
It is a different: if you develop in Netbeans or in Eclipse IDE; have a GlassFish or JBoss AppServer; use a Windows or Unix-based OS. Look for Tutorials, FAQ’s, Forums that match your enviromment. Finally spend time to get handy with it.
Some examples? Some students…
- …could find the log files from GlassFish (neighter on Windows nor on a Solaris Zone)
- …couldn’t read logfiles inside on a console (Example-Solution: “vi” or “less”)
- …didn’t use functions provided by the IDE (i.e. use manually wsimport, instead the UI-Wizard from the IDE, create manually getter/setter, etc.)
- …didn’t know they do can administrate an AppServer (i.e. neighter with the config-file nor with the Webgui)
Log files: Learn to read the log files
When you run into a problem, read the (error)log file(s). They were invented for that purpose. Maybe the are more than one. Look at all involved logs (IDE, AppSrv, Thirdparty, etc.). Look for known or self-created names (i.e. self-created classes) inside a stack trace and try to understand. Focus on the first occur error, mostly the followings depend on them.
Moreover: Don’t care only about errors, try to resolve the warnings, too!
The huge internet: Learn to use in Internet
It is tragically, I have to write something about this topic. But some times, I could answer a question with a simple “I’m Feeling Lucky” google-click! *grrr* Don’t wast my time or produce forum”spam” with trivial problems. Spend at least 20min searching the internet before you start to interrupt some other persons. Some very basic and not closing Tips:
- Don’t use self-created names (like class- or jndi names) in the search query
- The English speaking developing community is the biggest, therefore search in English
Further information are available here or here.
When you find example code, look to the version of it. Maybe is written for Java EE 1.4 or Java EE 6. If you couldn’t find a version hint, look for the creation date. Unfortunately, but mostly, you can mix different versions and standards inside your source code. The result could be a unpredictable and confusing behavior of the application. Then it would be hard to find the source of a problem – trust me, I spent (or waste) a lot of time with “un”mix students source code!
I don’t want to finish this post by leaving you with a bad impression of our students. As I already wrote in the beginning, they are also students they challenged me with very good questions, some made the developments (which is an attestation for the exam) without asking anything and lots did the job well.
