JavaBean is a way of building reusable components for Java applications. They are Java classes that follow certain naming conventions. There have been several JavaBean enhancements added in Java 7. Here we will focus on the java.beans.Expression class, which is useful in executing methods. The execute method has been added to facilitate this capability.
Getting ready
-----------------
To use the Expression class to execute a method:
How to do it...
-------------------
1. Create a new console application. Create two classes: JavaBeanExample, which contains the main method and a Person class. The Person class contains a single field for a name along with constructors, a getter method, and a setter method:
2. In the main method of the JavaBeanExample class, we will create an instance of the Person class, and use the Expression class to execute its getName and setName methods:
3. Execute the application. Its output should appear as follows:
Name: Taman
Name: Mohamed
Name: Mohamed
getValue: Mohamed
How it works...
-------------------
The Person class used a single field, name. The getName and setName methods were used from the main method, where a Person instance was created. The Expression class' constructor has four arguments. The first argument was not used in this example, but can be used to define a return value for the method executed. The second argument was the object that the method would be executed against. The third argument is a string containing the name of the method, and the last argument was an array containing the parameters used by the method.
In the first sequence, the setName method was executed using an argument of Mohamed. The output of the application shows that the name was initially Taman, but was changed to Mohamed after the execute method was executed.
In the second sequence, the getName method was executed. The getValue method returns the results of the execution of the method. The output shows that the getName method returned Mohamed.
There's more...
------------------
There have been other enhancements to the classes of the java.bean package. For example, the toString method has been overridden in the FeatureDescriptor and PropertyChangeEvent classes to provide a more meaningful description.
The Introspector class provides a way of learning about the properties, methods, and events of a Java Bean without using the Reflection API, which can be tedious. The class has added a getBeanInfo method, which uses the Inspector class' control flags to affect the BeanInfo object returned.
The Transient annotation has been added to control what is included. A true value for the attribute means that the annotated feature should be ignored.
A new constructor has been added to the XMLDecoder class that accepts an InputSource object. Also, the createHandler method has been added, which returns a DefaultHandler object. This handler is used to parse XML archives created by the XMLEncoder class.
A new constructor has been added to the XMLEncoder class. This permits writing out JavaBeans to an OutputStream using a specific charset with a specific indention.
Getting ready
-----------------
To use the Expression class to execute a method:
- Create an array of arguments for the method, if needed.
- Create an instance of the Expression class specifying the object that the method is to be executed against, the method name, and any arguments needed.
- Invoke the execute method against the expression.
- Use the getValue method to obtain the results of the method execution, if necessary.
How to do it...
-------------------
1. Create a new console application. Create two classes: JavaBeanExample, which contains the main method and a Person class. The Person class contains a single field for a name along with constructors, a getter method, and a setter method:
2. In the main method of the JavaBeanExample class, we will create an instance of the Person class, and use the Expression class to execute its getName and setName methods:
3. Execute the application. Its output should appear as follows:
Name: Taman
Name: Mohamed
Name: Mohamed
getValue: Mohamed
How it works...
-------------------
The Person class used a single field, name. The getName and setName methods were used from the main method, where a Person instance was created. The Expression class' constructor has four arguments. The first argument was not used in this example, but can be used to define a return value for the method executed. The second argument was the object that the method would be executed against. The third argument is a string containing the name of the method, and the last argument was an array containing the parameters used by the method.
In the first sequence, the setName method was executed using an argument of Mohamed. The output of the application shows that the name was initially Taman, but was changed to Mohamed after the execute method was executed.
In the second sequence, the getName method was executed. The getValue method returns the results of the execution of the method. The output shows that the getName method returned Mohamed.
There's more...
------------------
There have been other enhancements to the classes of the java.bean package. For example, the toString method has been overridden in the FeatureDescriptor and PropertyChangeEvent classes to provide a more meaningful description.
The Introspector class provides a way of learning about the properties, methods, and events of a Java Bean without using the Reflection API, which can be tedious. The class has added a getBeanInfo method, which uses the Inspector class' control flags to affect the BeanInfo object returned.
The Transient annotation has been added to control what is included. A true value for the attribute means that the annotated feature should be ignored.
A new constructor has been added to the XMLDecoder class that accepts an InputSource object. Also, the createHandler method has been added, which returns a DefaultHandler object. This handler is used to parse XML archives created by the XMLEncoder class.
A new constructor has been added to the XMLEncoder class. This permits writing out JavaBeans to an OutputStream using a specific charset with a specific indention.
What is the use of JavaBean?
ReplyDeleteIn this exemple, the main can be replaced like this
public static void main(String[] args) throws Exception {
Person person = new Person();
System.out.println("Name: " + person.getName());
person.setName("Mohamed");
System.out.println("Name: " + person.getName());
System.out.println();
System.out.println(
"Name: " + person.getName());
System.out.println(
"getValue: " + person.getName());
}
Dear i wrote this blog post for more info about JavaBeans check it and if there is any confusion please let me know.
DeleteJava: Why JavaBeans?
Dear Mohamed,
ReplyDeleteI was following your advice and searching for class java.beans.Expression using Google to get more information, but I found out that the Expression class is since Java 1.4
Just thought of sharing the info, too.
Dear Mohamed, you for sure correct that the Expression class since java 1.4 but as i post there are a lot of enhancements done in JavaBeans package in java 7, as in class Expression the execute method is new and added in java 7.
DeleteCheck this: http://docs.oracle.com/javase/7/docs/api/java/beans/Expression.html#execute()
Thanks dear for info.
Excellent way it easier than the reflection
ReplyDeletePlease tell me what's the correct way to get best performance,
if i used "Expression" in web application the best to annotate the class Person with @Stateless session bean or let it just class such as your example
or not different
Please i need your replay,
ReplyDeleteIs the best performance when i use "Expression" in web application to:
1- annotate the class Person with @Stateless session bean
or
2- just keep it a class like your example
or
3- there is no different between them
???
Dear sorry for late response,
DeleteIt will not affect your application performance.
Your options that you provide depend on what framework you use and architecture and they are different.
1- If you annotate your Person class with stateless session bean, you make your domain object also transaction aware, because it will be session bean and ejb component.
Examples:
1- You can make your application architecture MVC as the following JSF for presentation and its BackingBean acts as Controller and Stateless session bean at the same time and you manage the person domain object inside it. You don’t need to use Expression when using JSF with the person class because it makes the binding by default.
2- You can make your application architecture MVC as the following JSF for presentation and its BackingBean acts as Controller, and separate your backend Stateless session beans that manage the person inside it. Also here you don’t need to use Expression when using JSF with the person class because it makes the binding by default.
Think of using of Expression as in the following article Dynamic
search builder, the power of annotation, reflection and generics..
Thank you so much for replay
ReplyDeletebut i 'm so sorry i think you don't understand me,
My application is use Servlet for control , JSP for view, EJB 3, JPA
and i use one Servlet with url pattern "/faces/*" so any request to page after /faces/ will handle by this servlet then it will get path info from the request
Example:
when user request /faces/Products/select?product_id=001
then the Servlet that has url pattern "/faces/*" then the result of request.getPathInfo() will be /Products/select then use StringTokenizer on the result to get Products , select
Products = class name
select = method name
so i am using the reflection to initialize the class then use Method to invoke the method name
my question is in this example
i changed the reflection code to the Expression
so i want to know the best to annotate the Product class as Stateless session beans or keep it just class ?
Note:
i am not use jsf or any framework in this project
Ummm i got you now,
Deleteleave the Product as just a simple class since it is normal JPA entity.
if you annotate any class with Stateless session bean it will become part of EJB framework, and will be handled by EJB container.
OK OK
Deletebut actually the product is not an JPA entity class it's a controller class.
if you have some free time Please download the example and tell me what you think about Products class in faces.ctrl package
http://www.4shared.com/rar/MuBieC5Q/MyOwnWay.html
As you work at networking, make a point of traveling to conferences and business gatherings where industry bigwigs will be in attendance. Dress the part when you go to these conferences to ensure your appearance as a consummate professional worthy of hiring.
ReplyDeletefocus supplement