Saturday, May 26, 2012

JDK7: JavaBean enhancements in Java 7

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:
  1. Create an array of arguments for the method, if needed.

  2. 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.

  3. Invoke the execute method against the expression.

  4. 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.


  1. What is the use of JavaBean?
    In 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());

    System.out.println("Name: " + person.getName());

    "Name: " + person.getName());

    "getValue: " + person.getName());

    1. Dear i wrote this blog post for more info about JavaBeans check it and if there is any confusion please let me know.

      Java: Why JavaBeans?

  2. Dear Mohamed,
    I 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.

    1. 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.

      Check this:

      Thanks dear for info.

  3. Excellent way it easier than the reflection

    Please 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

  4. Please i need your replay,
    Is the best performance when i use "Expression" in web application to:
    1- annotate the class Person with @Stateless session bean
    2- just keep it a class like your example
    3- there is no different between them


    1. Dear sorry for late response,

      It 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.


      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.

  5. Thank you so much for replay

    but 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

    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 ?

    i am not use jsf or any framework in this project

    1. Ummm i got you now,

      leave 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.

    2. OK OK

      but 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

  6. 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.

    focus supplement