Accéder au contenu principal Passer au contenu complémentaire

Créer des Beans

Procédure

  1. Dans la vue Repository, développez le nœud Code > Global Beans et cliquez-droit sur le nœud Beans. Dans le menu contextuel, sélectionnez Create Bean.
  2. L'assistant New Bean s'ouvre. Dans le champ Name, saisissez un nom pour le Bean, Customer dans cet exemple.
  3. Saisissez le code suivant dans l'espace de modélisation graphique.
    package beans;
    
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlType;
    
    
    /**
     * <p>Java class for customer complex type.
     * 
     * <p>The following schema fragment specifies the expected content contained within this class.
     * 
     * <pre>
     * <complexType name="customer">
     *   <complexContent>
     *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
     *       <sequence>
     *         <element name="firstName" type="{http://www.w3.org/2001/XMLSchema}string"/>
     *         <element name="lastName" type="{http://www.w3.org/2001/XMLSchema}string"/>
     *         <element name="city" type="{http://www.w3.org/2001/XMLSchema}string"/>
     *       </sequence>
     *     </restriction>
     *   </complexContent>
     * </complexType>
     * </pre>
     * 
     * 
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "customer", propOrder = {
        "firstName",
        "lastName",
        "city"
    })
    @XmlRootElement(name = "customer")
    public class Customer {
    
        @XmlElement(required = true)
        protected String firstName;
        @XmlElement(required = true)
        protected String lastName;
        @XmlElement(required = true)
        protected String city;
    
        /**
         * Gets the value of the firstName property.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public String getFirstName() {
            return firstName;
        }
    
        /**
         * Sets the value of the firstName property.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setFirstName(String value) {
            this.firstName = value;
        }
    
        /**
         * Gets the value of the lastName property.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public String getLastName() {
            return lastName;
        }
    
        /**
         * Sets the value of the lastName property.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setLastName(String value) {
            this.lastName = value;
        }
    
        /**
         * Gets the value of the city property.
         * 
         * @return
         *     possible object is
         *     {@link String }
         *     
         */
        public String getCity() {
            return city;
        }
    
        /**
         * Sets the value of the city property.
         * 
         * @param value
         *     allowed object is
         *     {@link String }
         *     
         */
        public void setCity(String value) {
            this.city = value;
        }
    
    }
  4. Appuyez sur Ctrl+S pour sauvegarder votre Bean.
  5. Répétez cette opération pour créer un autre Bean nommé Customers à l'aide du code suivant :
    package beans;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    
    /**
     * <p>Java class for anonymous complex type.
     * 
     * <p>The following schema fragment specifies the expected content contained within this class.
     * 
     * <pre>
     * <complexType>
     *   <complexContent>
     *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
     *       <sequence>
     *         <element name="customer" type="{http://talend.org//services/esb/registry/soap/2012/09}customer" maxOccurs="unbounded" minOccurs="0"/>
     *       </sequence>
     *     </restriction>
     *   </complexContent>
     * </complexType>
     * </pre>
     * 
     * 
     */
    @XmlAccessorType(XmlAccessType.PROPERTY)
    @XmlRootElement(name = "customers")
    public class Customers {
    
    	private Map<Long, Customer> customers = new HashMap<Long, Customer>();
    	private long currentId = 1;
    	
        public static class Entry extends Customer {
            public Long id;
        }
    
        public Customers() {
        };
    
        /**
         * Gets the value of the customer property.
         * 
         * <p>
         * This accessor method returns a reference to the live list,
         * not a snapshot. Therefore any modification you make to the
         * returned list will be present inside the JAXB object.
         * This is why there is not a <CODE>set</CODE> method for the customer property.
         * 
         * <p>
         * For example, to add a new item, do as follows:
         * <pre>
         *    getCustomer().add(newItem);
         * </pre>
         * 
         * 
         * <p>
         * Objects of the following type(s) are allowed in the list
         * {@link Customer }
         * 
         * 
         */
        @XmlElement(name="customer")
        public List<Entry> getCustomers() {
        	List<Entry> adaptedMap = new ArrayList<Entry>();
            for(Map.Entry<Long, Customer> mapEntry : customers.entrySet()) {
                Entry entry = new Entry();
                entry.id = mapEntry.getKey();
                entry.setFirstName(mapEntry.getValue().getFirstName());
                entry.setLastName(mapEntry.getValue().getLastName());
                entry.setCity(mapEntry.getValue().getCity());
                adaptedMap.add(entry);
            }
            return adaptedMap;
        }
        
        public void setCustomers(List<Entry> adaptedMap) {
            for(Entry entry : adaptedMap) {
                customers.put(entry.id, entry);
            }
        }
    
        public Customer getCustomer(long id) {
        	return customers.get(id);
        }
    
        public long addCustomer(Customer customer) {
        	customers.put(currentId, customer);
        	return currentId++;
        }
        
        public void updateCustomer(Long id, Customer customer) {
       		customers.put(id, customer);
        }
        
        public void deleteCustomer(long id) {
    		customers.remove(id);
        }
        
    }

    Pour plus d'informations concernant la création et l'utilisation de Beans Java, consultez Utilisation des Beans.

Cette page vous a-t-elle aidé ?

Si vous rencontrez des problèmes sur cette page ou dans son contenu – une faute de frappe, une étape manquante ou une erreur technique – dites-nous comment nous améliorer !