Java BeanUtils - Accès aux propriétés de base

La description

Vous pouvez accéder aux propriétés de base en utilisant les méthodes suivantes:

  • Propriété simple

  • Propriété indexée

  • Propriété mappée

Propriété simple

Vous pouvez obtenir et définir le simple valeurs de propriété en utilisant les signatures API ci-dessous:

  • PropertyUtils.getSimpleProperty (objet, chaîne)

  • PropertyUtils.SetSimpleProperty (objet, chaîne, objet)

Paramètres:

  • Object: C'est un objet bean qui spécifie la propriété bean à extraire.

  • String: C'est un nom de chaîne qui spécifie le nom de la propriété à extraire.

Propriété indexée

Vous pouvez utiliser deux options pour créer indexedPropriétés; La première option consiste à construire l'indice dans le nom de la propriété et la deuxième option définit l'indice dans un argument séparé pour appeler la méthode.

Les propriétés indexées peuvent être obtenues et définies à l'aide des méthodes ci-dessous:

  • PropertyUtils.getIndexedProperty (objet, chaîne)

  • PropertyUtils.getIndexedProperty (Objet, Chaîne, entier)

  • PropertyUtils.setIndexedProperty (objet, chaîne, objet)

  • PropertyUtils.setIndexedProperty (objet, chaîne, int, objet)

Paramètres:

  • Object: C'est un objet bean qui spécifie la propriété bean à extraire.

  • String: C'est un nom de chaîne qui spécifie le nom de la propriété à extraire.

  • int: Il définit un index de la valeur de la propriété.

  • Object: Il spécifie la valeur d'un élément de propriété indexé.

Propriété mappée

Vous pouvez obtenir et définir le mappedvaleurs de propriété en utilisant les signatures API ci-dessous. Si vous avez un argument supplémentaire, alors il peut être écrit entre parenthèses sous la forme ("(" et ")") au lieu d'utiliser des crochets.

  • PropertyUtils.getMappedProperty (objet, chaîne)

  • PropertyUtils.getMappedProperty (objet, chaîne, chaîne)

  • PropertyUtils.setMappedProperty (Objet, Chaîne, Objet)

  • PropertyUtils.setMappedProperty (objet, chaîne, chaîne, objet)

Paramètres:

  • Object: C'est un objet bean qui spécifie la propriété bean à extraire.

  • String: C'est un nom de la valeur de propriété qui doit être définie pour la propriété mappée.

  • String: Il définit la clé de la valeur de propriété à définir.

  • Object: Il spécifie la valeur de la propriété à définir.

Exemple

L'exemple ci-dessous illustre l'utilisation des propriétés ci-dessus dans les beanUtils:

import org.apache.commons.beanutils.PropertyUtils;
import java.util.ArrayList;
import java.util.List;

public class BeanUtilsPropertyDemo{
   public static void main(String args[]){

   try{
      // Creating the bean and allows to access getter and setter properties
      MyBean myBean = new MyBean();

      // Setting the properties on the myBean
      PropertyUtils.setSimpleProperty(myBean, "stringProp", "Hello!This is a string");
      PropertyUtils.setSimpleProperty(myBean, "floatProp", new Float(25.20));

      // Getting the simple properties
      System.out.println("String Property: " + PropertyUtils.getSimpleProperty(myBean, "stringProp"));

      System.out.println("Float Property: " + PropertyUtils.getSimpleProperty(myBean, "floatProp"));

      // Here we will create a list for the indexed property
      List
      
        list = new ArrayList
       
        (); list.add("String value 0"); list.add("String value 1"); myBean.setListProp(list); // get and set this indexed property PropertyUtils.setIndexedProperty(myBean, "listProp[1]", "This is new string value 1"); System.out.println("List Property[1]: " + PropertyUtils.getIndexedProperty(myBean, "listProp[1]")); }catch(Exception e){ System.out.println(e); } } } 
       
      

Now we will create one more class called MyBean.java for the bean class:

import java.util.ArrayList;
import java.util.List;

public class MyBean {
   private String stringProp;
   private float floatProp;

   //indexed property
   @SuppressWarnings("rawtypes")
   private List listProp = new ArrayList();

   public void setStringProp(String stringProp) { this.stringProp = stringProp; }
   public String getStringProp() { return this.stringProp; }

   public void setFloatProp(float floatProp) { this.floatProp = floatProp; }
   public float getFloatProp() { return this.floatProp; }

   public void setListProp(List<?> listProp) { this.listProp = listProp; }
   public List<?> getListProp() { return this.listProp; }
	}

Output

Let's carry out the following steps to see how above code works:

  • Save the above first code as BeanUtilsPropertyDemo.java.

  • Now execute the code using Run option or Ctrl+f11 and output as below gets displayed.

Basic Property Access