Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Features  





2 Advantages  





3 Disadvantages  





4 JavaBeans API  





5 JavaBean conventions  



5.1  Code example  







6 See also  





7 References  





8 External links  














JavaBeans






Català
Čeština
Deutsch
Español
Français

Հայերեն
Interlingua
Italiano
עברית
Magyar
Nederlands

Polski
Português
Русский
کوردی
Suomi

Українська

 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


In computing based on the Java Platform, JavaBeans is a technology developed by Sun Microsystems and released in 1996, as part of JDK 1.1.

The 'beans' of JavaBeans are classes that encapsulate one or more objects into a single standardized object (the bean). This standardization allows the beans to be handled in a more generic fashion, allowing easier code reuse and introspection. This in turn allows the beans to be treated as software components, and to be manipulated visually by editors and IDEs without needing any initial configuration, or to know any internal implementation details.

As part of the standardization, all beans must be serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods.

Features[edit]

Introspection
Introspection is a process of analyzing a Bean to determine its capabilities. This is an essential feature of the Java Beans specification because it allows another application, such as a design tool, to obtain information about a component.
Properties
A property is a subset of a Bean's state. The values assigned to the properties determine the behaviour and appearance of that component. They are set through a setter method and can be obtained by a getter method.
Customization
A customizer can provide a step-by-step guide that the process must follow to use the component in a specific context.
Events
Beans may interact with the EventObject EventListener model.[clarification needed]
Persistence
Persistence is the ability to save the current state of a Bean, including the values of a Bean's properties and instance variables, to nonvolatile storage and to retrieve them at a later time.
Methods
A Bean should use accessor methodstoencapsulate the properties. A Bean can provide other methods for business logic not related to the access to the properties.

Advantages[edit]

Disadvantages[edit]

JavaBeans API[edit]

The JavaBeans functionality is provided by a set of classes and interfaces in the java.beans package.

Interface Description
AppletInitializer Methods in this interface are used to initialize Beans that are also applets.
BeanInfo This interface allows the designer to specify information about the events, methods and properties of a Bean.
Customizer This interface allows the designer to provide a graphical user interface through which a bean may be configured.
DesignMode Methods in this interface determine if a bean is executing in design mode.
ExceptionListener A method in this interface is invoked when an exception has occurred.
PropertyChangeListener A method in this interface is invoked when a bound property is changed.
PropertyEditor Objects that implement this interface allow the designer to change and display property values.
VetoableChangeListener A method in this interface is invoked when a Constrained property is changed.
Visibility Methods in this interface allow a bean to execute in environments where the GUI is not available.

JavaBean conventions[edit]

In order to function as a JavaBean class, an object class must obey certain conventions about method naming, construction, and behaviour. These conventions make it possible to have tools that can use, reuse, replace, and connect Java Beans.

The required conventions are as follows:

Code example[edit]

package player;

public class PersonBean implements java.io.Serializable {

    /** Properties **/
    private boolean deceased = false;

    private List list;

    /** Property "name", readable/writable. */
    private String name = null;

    /** No-arg constructor (takes no arguments). */
    public PersonBean() {
    }

    public List getList() {
        return list;
    }
 
    public void setList(final List list) {
        this.list = list;
    }

    /**
     * Getter for property "name".
     */
    public String getName() {
        return name;
    }

    /**
     * Setter for property "name".
     *
     * @param value
     */
    public void setName(final String value) {
        this.name = value;
    }

    /**
     * Getter for property "deceased"
     * Different syntax for a boolean field (is vs get)
     */
    public boolean isDeceased() {
        return deceased;
    }

    /**
     * Setter for property "deceased".
     * @param value
     */
    public void setDeceased(boolean value) {
        deceased = value;
    }
}

TestPersonBean.java:

import player.PersonBean;

/**
 * Class "TestPersonBean".
 */
public class TestPersonBean {
    /**
     * Tester method "main" for class "PersonBean".
     *
     * @param arguments
     */
    public static void main(final String[] arguments) {
        final PersonBean person = new PersonBean();

        person.setName("Bob");
        person.setDeceased(false);
        person.setList(new ArrayList());

        // Output: "Bob [alive]"
        System.out.print(person.getName());
        System.out.println(person.isDeceased() ? " [deceased]" : " [alive]");
    }
}
<jsp:useBean id="person" class="player.PersonBean" scope="page"/>
<jsp:setProperty name="person" property="*"/>

<html>
    <body>
        Name: <jsp:getProperty name="person" property="name"/><br/>
        Deceased? <jsp:getProperty name="person" property="deceased"/><br/>
        <br/>
        <form name="beanTest" method="POST" action="testPersonBean.jsp">
            Enter a name: <input type="text" name="name" size="50"><br/>
            Choose an option:
            <select name="deceased">
                <option value="false">Alive</option>
                <option value="true">Dead</option>
            </select>
            <input type="submit" value="Test the Bean">
        </form>
    </body>
</html>

See also[edit]

References[edit]

  1. ^ a b Bloch, Joshua (2008). Effective Java (Second ed.). Addison-Wesley. p. 13. ISBN 978-0-321-35668-0.

External links[edit]


Retrieved from "https://en.wikipedia.org/w/index.php?title=JavaBeans&oldid=1166618058"

Categories: 
Java platform
Architectural pattern (computer science)
Software design patterns
Hidden categories: 
Articles with short description
Short description is different from Wikidata
Articles needing additional references from June 2016
All articles needing additional references
Wikipedia articles with style issues from October 2012
All articles with style issues
Articles with multiple maintenance issues
Wikipedia articles needing clarification from May 2020
All articles with unsourced statements
Articles with unsourced statements from January 2023
Articles with example Java code
 



This page was last edited on 22 July 2023, at 18:49 (UTC).

Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



Privacy policy

About Wikipedia

Disclaimers

Contact Wikipedia

Code of Conduct

Developers

Statistics

Cookie statement

Mobile view



Wikimedia Foundation
Powered by MediaWiki