Creating Beans
Procedure
-
From the repository tree view, expand Code > Global Beans and right-click the Beans node. In
the contextual menu, select Create Bean.
- The New Bean wizard opens. In the Name field, type in a name for the Bean, Customer in this use case.
-
Enter the following code in the design workspace.
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; } }
- Press Ctrl+S to save your Bean.
-
Repeat this operation to create another Bean named Customers
using the following code:
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); } }
For more information about creating and using Java Beans, see Using Beans.
Did this page help you?
If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!