SlideShare una empresa de Scribd logo
1 de 32
JavaBeans

   An introduction to component-based
    development in general
   Introduction to JavaBeans
    –   Java components
    –   client-side
   Working with the BDK
   The beans development life cycle
   Writing simple and advanced beans
                Copyright © 2001 Qusay H. Mahmoud
Software Components

   All engineering discplines use components to
    build systems. In SE we rely on line-by-line SD.
   We have class libraries
    –   create objects from class libraries
    –   we still need to write a large amount of code
    –   objects are not enough




                  Copyright © 2001 Qusay H. Mahmoud
Software Components

   They are like Integrated Circuit (IC)
    components
   Over 20 years ago, hardware vendors learned
    how to package transistors
   Hardware Engineers integrate ICs to make a
    board of chips
   In SE, we are where hardware engineers were
    20 years ago
   We are building software routines
              Copyright © 2001 Qusay H. Mahmoud
Java Components

   Instead of worrying about rotuines, we can buy
    routines and use/reuse them in our
    applications (assemble applications)
   JavaBeans -- portable, platform-independent
    component model
   Java components are known as beans
   A bean: a reusable software component that
    can be manipulated visually in a builder tool

              Copyright © 2001 Qusay H. Mahmoud
JavaBeans vs. Class Libraries

   Beans are appropriate for software
    components that can be visually manipulated

   Class libraries are good for providing
    functionality that is useful to programmers, and
    doesn’t benefit from visual manipulation



               Copyright © 2001 Qusay H. Mahmoud
JavaBeans Concepts

   A component is a self-contained reusable
    software unit
   Components expose their features (public
    methods and events) to builder tools
   A builder tool maintains Beans in a palette or
    toolbox.



               Copyright © 2001 Qusay H. Mahmoud
Concepts...

   You can select a bean from the toolbox, drop it
    in a form, and modify its appearance and
    behavior.
   Also, you can define its interaction with other
    beans
   ALL this without a line of code.



               Copyright © 2001 Qusay H. Mahmoud
JavaBean Characteristics

   a public class with 0-argument constuctor
   it has properties with accessory methods
   it has events
   it can customized
   its state can be saved
   it can be analyzed by a builder tool



               Copyright © 2001 Qusay H. Mahmoud
Key Concepts

   A builder tool discover a bean’s features by a
    process known as introspection.
    –   Adhering to specific rules (design pattern) when
        naming Bean features.
    –   Providing property, method, and event information
        with a related Bean Information class.
   Properties (bean’s appearance and behavior
    characteristics) can be changed at design-time.

                 Copyright © 2001 Qusay H. Mahmoud
Key Concepts….

   Properties can be customized at design-time.
    Customization can be done:
    –   using property editor
    –   using bean customizers
   Events are used when beans want to
    intercommunicate
   Persistence: for saving and restoring the state
   Bean’s methods are regular Java methods.

                 Copyright © 2001 Qusay H. Mahmoud
Security Issues

   JavaBeans are sbject to the standard Java
    security model
   The security model has neither extended nor
    relaxed.
   If a bean runs as an untrusted applet then it will
    be subject to applet security
   If a bean runs as a stand-alone application
    then it will be treated as a normal Java
    application.
               Copyright © 2001 Qusay H. Mahmoud
JavaBeans and Threads

   Assume your beans will be running in a multi-
    threaded environment
   It is your responsibility (the developer) to make
    sure that their beans behave properly under
    multi-threaded access
   For simple beans, this can be handled by
    simply making all methods …...


               Copyright © 2001 Qusay H. Mahmoud
Beans Development Kit (BDK)

   To start the BeanBox:
    –   run.bat (Windows)
    –   run.sh (Unix)




                 Copyright © 2001 Qusay H. Mahmoud
BDK

   ToolBox contains the beans available
   BeanBox window is the form where you
    visually wire beans together.
   Properties sheet: displays the properties for the
    Bean currently selected within the BeanBox
    window.



               Copyright © 2001 Qusay H. Mahmoud
MyFirstBean

   import java.awt.*;
   import java.io.Serializable;
   public class FirstBean extends Canvas implements
    Serializable {
      public FirstBean() {
       setSize(50,30);
       setBackground(Color.blue);
     }
   }
                Copyright © 2001 Qusay H. Mahmoud
First Bean

   Compile: javac FirstBean.java
   Create a manifest file:
   manifest.txt
    –   Name: FirstBean.class
    –   Java-Bean: True
   Create a jar file:
   jar cfm FirstBean.jar mani.txt FirstBean.class


                 Copyright © 2001 Qusay H. Mahmoud
Using Beans in hand-written app

   Use Beans.instantiate
      Frame f;
      f = new Frame("Testing Beans");
      try {
       ClassLoader cl = this.getClass().getClassLoader();
       fb =(FirstBean)Beans.instantiate(cl,"FirstBean");
      } catch(Exception e) {
        e.printStackTrace();
      }
     f.add(fb);

                 Copyright © 2001 Qusay H. Mahmoud
Properties

   Bean’s appearance and behavior --
    changeable at design time.
   They are private values
   Can be accessed through getter and setter
    methods
   getter and setter methods must follow some
    rules -- design patterns (documenting
    experience)

              Copyright © 2001 Qusay H. Mahmoud
Properties

   A builder tool can:
    –   discover a bean’s properties
    –   determine the properties’ read/write attribute
    –   locate an appropriate “property editor” for each type
    –   display the properties (in a sheet)
    –   alter the properties at design-time




                  Copyright © 2001 Qusay H. Mahmoud
Types of Properties

   Simple
   Index: multiple-value properties
   Bound: provide event notification when value
    changes
   Constrained: how proposed changes can be
    okayed or vetoed by other object



              Copyright © 2001 Qusay H. Mahmoud
Simple Properties

   When a builder tool introspect your bean it
    discovers two methods:
    –   public Color getColor()
    –   public void setColor(Color c)
   The builder tool knows that a property named
    “Color” exists -- of type Color.
   It tries to locate a property editor for that type
    to display the properties in a sheet.

                  Copyright © 2001 Qusay H. Mahmoud
Simple Properties….

   Adding a Color property
    –   Create and initialize a private instance variable
            private Color color = Color.blue;
    –   Write public getter & setter methods
            public Color getColor() {
                 –   return color;
            }
            public void setColor(Color c) {
                 –   color = c;
                 –   repaint();
            }

                          Copyright © 2001 Qusay H. Mahmoud
Events “Introspection”

   For a bean to be the source of an event, it
    must implement methods that add and remove
    listener objects for the type of the event:
    –   public void add<EventListenerType>(<EventListenerType> elt);
    –   same thing for remove
   These methods help a source Bean know
    where to fire events.



                    Copyright © 2001 Qusay H. Mahmoud
Events “Introspection”

   Source Bean fires events at the listeners using
    method of those interfaces.
   Example: if a source Bean register
    ActionListsener objects, it will fire events at
    those objects by calling the actionPerformed
    method on those listeners



               Copyright © 2001 Qusay H. Mahmoud
Events “using BeanInfo”

   Implementing the BeanInfo interface allows
    you to explicitly publish the events a Bean fires




               Copyright © 2001 Qusay H. Mahmoud
BeanInfo interface

   Question: how does a Bean exposes its
    features in a property sheet?
   Answer: using java.beans.Introspector class
    (which uses Core Reflection API)
   The discovery process is named “introspection”
   OR you can associate a class that implements
    the BeanInfo with your bean


              Copyright © 2001 Qusay H. Mahmoud
BeanInfo interface….

   Why use BeanInfo then?
   Using BeanInfo you can:
    –   Expose features that you want to expose




                 Copyright © 2001 Qusay H. Mahmoud
Bean Customization

   The appearance and behavior of a bean can
    be customized at design time.
   Two ways to customize a bean:
    –   using a property editor
            each bean property has its own editor
            a bean’s property is displayed in a property sheet
    –   using customizers
            gives you complete GUI control over bean customization
            used when property editors are not practical

                     Copyright © 2001 Qusay H. Mahmoud
Property Editors

   A property editor is a user interface for editing
    a bean property. The property must have both,
    read/write accessor methods.
   A property editor must implement the
    PropertyEditor interface.
   PropertyEditorSupport does that already, so
    you can extend it.


               Copyright © 2001 Qusay H. Mahmoud
Property Editors

   If you provide a custom property editor class,
    then you must refer to this class by calling
    PropertyDescriptor.setPropertyEditorClass in a
    BeanInfo class.
   Each bean may have a BeanInfo class which
    customizes how the bean is to appear.
    SimpleBeanInfo implements that interface


              Copyright © 2001 Qusay H. Mahmoud
How to be a good bean?

   JavaBeans are just the start of the Software
    Components industry.
   This market is growing in both, quantity and
    quality.
   To promote commercial quality java beans
    components and tools, we should strive to
    make our beans as reusable as possible.
   Here are a few guidelines...

               Copyright © 2001 Qusay H. Mahmoud
How to be a good bean?

   Creating beans
    –   Your bean class must provide a zero-argument
        constructor. So, objects can be created using
        Bean.instantiate();
    –   The bean must support persistence
            implement Serializable or Externalizable




                     Copyright © 2001 Qusay H. Mahmoud

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 
Java swing
Java swingJava swing
Java swing
 
Java beans
Java beansJava beans
Java beans
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
Spring notes
Spring notesSpring notes
Spring notes
 
Java packages
Java packagesJava packages
Java packages
 
.Net Assemblies
.Net Assemblies.Net Assemblies
.Net Assemblies
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Introduction à spring boot
Introduction à spring bootIntroduction à spring boot
Introduction à spring boot
 
Unit iv
Unit ivUnit iv
Unit iv
 
Basics of JAVA programming
Basics of JAVA programmingBasics of JAVA programming
Basics of JAVA programming
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
Layout manager
Layout managerLayout manager
Layout manager
 
Introduction to ASP.NET
Introduction to ASP.NETIntroduction to ASP.NET
Introduction to ASP.NET
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 

Similar a Javabeans

CommercialSystemsBahman.ppt
CommercialSystemsBahman.pptCommercialSystemsBahman.ppt
CommercialSystemsBahman.ppt
KalsoomTahir2
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions
Dhiraj Champawat
 
User interface customization for aem6 circuit
User interface customization for aem6 circuitUser interface customization for aem6 circuit
User interface customization for aem6 circuit
Damien Antipa
 

Similar a Javabeans (20)

introduction of Java beans
introduction of Java beansintroduction of Java beans
introduction of Java beans
 
Unit4wt
Unit4wtUnit4wt
Unit4wt
 
Unit4wt
Unit4wtUnit4wt
Unit4wt
 
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
 
Java Beans Unit 4(part 2)
Java Beans Unit 4(part 2)Java Beans Unit 4(part 2)
Java Beans Unit 4(part 2)
 
Javabeans .pdf
Javabeans .pdfJavabeans .pdf
Javabeans .pdf
 
Java beans
Java beansJava beans
Java beans
 
Javabean1
Javabean1Javabean1
Javabean1
 
Easy as pie creating widgets for ibm connections
Easy as pie   creating widgets for ibm connectionsEasy as pie   creating widgets for ibm connections
Easy as pie creating widgets for ibm connections
 
CommercialSystemsBahman.ppt
CommercialSystemsBahman.pptCommercialSystemsBahman.ppt
CommercialSystemsBahman.ppt
 
Bean Intro
Bean IntroBean Intro
Bean Intro
 
Features everywhere
Features everywhere Features everywhere
Features everywhere
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Java beans
Java beansJava beans
Java beans
 
02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions02 java spring-hibernate-experience-questions
02 java spring-hibernate-experience-questions
 
javabeans
javabeansjavabeans
javabeans
 
L0043 - Interfacing to Eclipse Standard Views
L0043 - Interfacing to Eclipse Standard ViewsL0043 - Interfacing to Eclipse Standard Views
L0043 - Interfacing to Eclipse Standard Views
 
Code camp 2011 Getting Started with IOS, Una Daly
Code camp 2011 Getting Started with IOS, Una DalyCode camp 2011 Getting Started with IOS, Una Daly
Code camp 2011 Getting Started with IOS, Una Daly
 
UI Customization in AEM 6.0
UI Customization in AEM 6.0UI Customization in AEM 6.0
UI Customization in AEM 6.0
 
User interface customization for aem6 circuit
User interface customization for aem6 circuitUser interface customization for aem6 circuit
User interface customization for aem6 circuit
 

Más de vamsitricks (20)

Unit 6
Unit 6Unit 6
Unit 6
 
Np unit2
Np unit2Np unit2
Np unit2
 
Np unit1
Np unit1Np unit1
Np unit1
 
Np unit iv i
Np unit iv iNp unit iv i
Np unit iv i
 
Np unit iii
Np unit iiiNp unit iii
Np unit iii
 
Np unit iv ii
Np unit iv iiNp unit iv ii
Np unit iv ii
 
Unit 7
Unit 7Unit 7
Unit 7
 
Npc16
Npc16Npc16
Npc16
 
Npc14
Npc14Npc14
Npc14
 
Npc13
Npc13Npc13
Npc13
 
Npc08
Npc08Npc08
Npc08
 
Unit 3
Unit 3Unit 3
Unit 3
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 2
Unit 2Unit 2
Unit 2
 
Unit 7
Unit 7Unit 7
Unit 7
 
Unit 6
Unit 6Unit 6
Unit 6
 
Unit 4
Unit 4Unit 4
Unit 4
 
Unit3wt
Unit3wtUnit3wt
Unit3wt
 
Unit2wt
Unit2wtUnit2wt
Unit2wt
 
Unit 1wt
Unit 1wtUnit 1wt
Unit 1wt
 

Javabeans

  • 1. JavaBeans  An introduction to component-based development in general  Introduction to JavaBeans – Java components – client-side  Working with the BDK  The beans development life cycle  Writing simple and advanced beans Copyright © 2001 Qusay H. Mahmoud
  • 2. Software Components  All engineering discplines use components to build systems. In SE we rely on line-by-line SD.  We have class libraries – create objects from class libraries – we still need to write a large amount of code – objects are not enough Copyright © 2001 Qusay H. Mahmoud
  • 3. Software Components  They are like Integrated Circuit (IC) components  Over 20 years ago, hardware vendors learned how to package transistors  Hardware Engineers integrate ICs to make a board of chips  In SE, we are where hardware engineers were 20 years ago  We are building software routines Copyright © 2001 Qusay H. Mahmoud
  • 4. Java Components  Instead of worrying about rotuines, we can buy routines and use/reuse them in our applications (assemble applications)  JavaBeans -- portable, platform-independent component model  Java components are known as beans  A bean: a reusable software component that can be manipulated visually in a builder tool Copyright © 2001 Qusay H. Mahmoud
  • 5. JavaBeans vs. Class Libraries  Beans are appropriate for software components that can be visually manipulated  Class libraries are good for providing functionality that is useful to programmers, and doesn’t benefit from visual manipulation Copyright © 2001 Qusay H. Mahmoud
  • 6. JavaBeans Concepts  A component is a self-contained reusable software unit  Components expose their features (public methods and events) to builder tools  A builder tool maintains Beans in a palette or toolbox. Copyright © 2001 Qusay H. Mahmoud
  • 7. Concepts...  You can select a bean from the toolbox, drop it in a form, and modify its appearance and behavior.  Also, you can define its interaction with other beans  ALL this without a line of code. Copyright © 2001 Qusay H. Mahmoud
  • 8. JavaBean Characteristics  a public class with 0-argument constuctor  it has properties with accessory methods  it has events  it can customized  its state can be saved  it can be analyzed by a builder tool Copyright © 2001 Qusay H. Mahmoud
  • 9. Key Concepts  A builder tool discover a bean’s features by a process known as introspection. – Adhering to specific rules (design pattern) when naming Bean features. – Providing property, method, and event information with a related Bean Information class.  Properties (bean’s appearance and behavior characteristics) can be changed at design-time. Copyright © 2001 Qusay H. Mahmoud
  • 10. Key Concepts….  Properties can be customized at design-time. Customization can be done: – using property editor – using bean customizers  Events are used when beans want to intercommunicate  Persistence: for saving and restoring the state  Bean’s methods are regular Java methods. Copyright © 2001 Qusay H. Mahmoud
  • 11. Security Issues  JavaBeans are sbject to the standard Java security model  The security model has neither extended nor relaxed.  If a bean runs as an untrusted applet then it will be subject to applet security  If a bean runs as a stand-alone application then it will be treated as a normal Java application. Copyright © 2001 Qusay H. Mahmoud
  • 12. JavaBeans and Threads  Assume your beans will be running in a multi- threaded environment  It is your responsibility (the developer) to make sure that their beans behave properly under multi-threaded access  For simple beans, this can be handled by simply making all methods …... Copyright © 2001 Qusay H. Mahmoud
  • 13. Beans Development Kit (BDK)  To start the BeanBox: – run.bat (Windows) – run.sh (Unix) Copyright © 2001 Qusay H. Mahmoud
  • 14. BDK  ToolBox contains the beans available  BeanBox window is the form where you visually wire beans together.  Properties sheet: displays the properties for the Bean currently selected within the BeanBox window. Copyright © 2001 Qusay H. Mahmoud
  • 15. MyFirstBean  import java.awt.*;  import java.io.Serializable;  public class FirstBean extends Canvas implements Serializable {  public FirstBean() {  setSize(50,30);  setBackground(Color.blue);  }  } Copyright © 2001 Qusay H. Mahmoud
  • 16. First Bean  Compile: javac FirstBean.java  Create a manifest file:  manifest.txt – Name: FirstBean.class – Java-Bean: True  Create a jar file:  jar cfm FirstBean.jar mani.txt FirstBean.class Copyright © 2001 Qusay H. Mahmoud
  • 17. Using Beans in hand-written app  Use Beans.instantiate  Frame f;  f = new Frame("Testing Beans");  try {  ClassLoader cl = this.getClass().getClassLoader();  fb =(FirstBean)Beans.instantiate(cl,"FirstBean");  } catch(Exception e) {  e.printStackTrace();  }  f.add(fb); Copyright © 2001 Qusay H. Mahmoud
  • 18. Properties  Bean’s appearance and behavior -- changeable at design time.  They are private values  Can be accessed through getter and setter methods  getter and setter methods must follow some rules -- design patterns (documenting experience) Copyright © 2001 Qusay H. Mahmoud
  • 19. Properties  A builder tool can: – discover a bean’s properties – determine the properties’ read/write attribute – locate an appropriate “property editor” for each type – display the properties (in a sheet) – alter the properties at design-time Copyright © 2001 Qusay H. Mahmoud
  • 20. Types of Properties  Simple  Index: multiple-value properties  Bound: provide event notification when value changes  Constrained: how proposed changes can be okayed or vetoed by other object Copyright © 2001 Qusay H. Mahmoud
  • 21. Simple Properties  When a builder tool introspect your bean it discovers two methods: – public Color getColor() – public void setColor(Color c)  The builder tool knows that a property named “Color” exists -- of type Color.  It tries to locate a property editor for that type to display the properties in a sheet. Copyright © 2001 Qusay H. Mahmoud
  • 22. Simple Properties….  Adding a Color property – Create and initialize a private instance variable  private Color color = Color.blue; – Write public getter & setter methods  public Color getColor() { – return color;  }  public void setColor(Color c) { – color = c; – repaint();  } Copyright © 2001 Qusay H. Mahmoud
  • 23. Events “Introspection”  For a bean to be the source of an event, it must implement methods that add and remove listener objects for the type of the event: – public void add<EventListenerType>(<EventListenerType> elt); – same thing for remove  These methods help a source Bean know where to fire events. Copyright © 2001 Qusay H. Mahmoud
  • 24. Events “Introspection”  Source Bean fires events at the listeners using method of those interfaces.  Example: if a source Bean register ActionListsener objects, it will fire events at those objects by calling the actionPerformed method on those listeners Copyright © 2001 Qusay H. Mahmoud
  • 25. Events “using BeanInfo”  Implementing the BeanInfo interface allows you to explicitly publish the events a Bean fires Copyright © 2001 Qusay H. Mahmoud
  • 26. BeanInfo interface  Question: how does a Bean exposes its features in a property sheet?  Answer: using java.beans.Introspector class (which uses Core Reflection API)  The discovery process is named “introspection”  OR you can associate a class that implements the BeanInfo with your bean Copyright © 2001 Qusay H. Mahmoud
  • 27. BeanInfo interface….  Why use BeanInfo then?  Using BeanInfo you can: – Expose features that you want to expose Copyright © 2001 Qusay H. Mahmoud
  • 28. Bean Customization  The appearance and behavior of a bean can be customized at design time.  Two ways to customize a bean: – using a property editor  each bean property has its own editor  a bean’s property is displayed in a property sheet – using customizers  gives you complete GUI control over bean customization  used when property editors are not practical Copyright © 2001 Qusay H. Mahmoud
  • 29. Property Editors  A property editor is a user interface for editing a bean property. The property must have both, read/write accessor methods.  A property editor must implement the PropertyEditor interface.  PropertyEditorSupport does that already, so you can extend it. Copyright © 2001 Qusay H. Mahmoud
  • 30. Property Editors  If you provide a custom property editor class, then you must refer to this class by calling PropertyDescriptor.setPropertyEditorClass in a BeanInfo class.  Each bean may have a BeanInfo class which customizes how the bean is to appear. SimpleBeanInfo implements that interface Copyright © 2001 Qusay H. Mahmoud
  • 31. How to be a good bean?  JavaBeans are just the start of the Software Components industry.  This market is growing in both, quantity and quality.  To promote commercial quality java beans components and tools, we should strive to make our beans as reusable as possible.  Here are a few guidelines... Copyright © 2001 Qusay H. Mahmoud
  • 32. How to be a good bean?  Creating beans – Your bean class must provide a zero-argument constructor. So, objects can be created using Bean.instantiate(); – The bean must support persistence  implement Serializable or Externalizable Copyright © 2001 Qusay H. Mahmoud