JavaBeans (and POJOs) - Basic Java components

aniseedsplashΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 5 χρόνια και 29 μέρες)

388 εμφανίσεις

UFCE4Y
-
20
-
3

Components

Julia Dawson

16/03/2013

UFCE4Y
-
20
-
3

1




JavaBeans (and
POJOs
)




Basic
Java components


16/03/2013

UFCE4Y
-
20
-
3

2

Why JavaBeans?


The JavaBeans component architecture is the
platform
-
neutral architecture for the Java application
environment. It's the ideal choice for developing or
assembling network
-
aware solutions for
heterogeneous hardware and operating system
environments
--
within the enterprise or across the
Internet.



http://java.sun.com/developer/onlineTraining/Beans/Beans
1/simple
-
definition.html



http://en.wikipedia.org/wiki/JavaBean




16/03/2013

UFCE4Y
-
20
-
3

3

POJO


“I’ve come to the conclusion that people forget
about regular Java objects because they haven’t
got a fancy name
-

so while preparing for a talk
Rebecca Parsons, Josh Mackenzie and I gave
them one: POJO (Plain Old Java Object).”


“A POJO domain model is easier to put together,
quick to build, can run and test outside of an
EJB container, and isn’t dependent on EJB”

~ Martin Fowler

16/03/2013

UFCE4Y
-
20
-
3

4

Why JavaBeans?

The JavaBeans API discussion began with a reminder that M. D.
McIlroy (1968) made a plea for catalogs of software components
more than 30 years ago. JavaBeans, of course,
is

the standard
component architecture for Java technology.




It's clear that there is now, finally, a strong movement toward
component
-
oriented design and implementation. Some enterprises
dictate, for example, that every Java class be designed and
implemented as a JavaBeans component. In these enterprises,
programming groups exchange and share their software
developments as Beans

they use each other's work in a plug
-
and
-
play application development setting.




developer.java.sun.com/developer/onlineTraining/Beans/JBeansAPI/shortcourse.html

16/03/2013

UFCE4Y
-
20
-
3

5

The JavaBeans API


The JavaBeans API makes it possible to write
component software in the Java programming
language.


Components are self
-
contained, reusable
software units that can be visually composed
into composite components, applets,
applications, and servlets using visual
application builder tools.


JavaBean components are known as
Beans
.

16/03/2013

UFCE4Y
-
20
-
3

6

JavaBeans / Java Classes ?


Beans are Java classes that can be
manipulated

in a visual builder tool and
composed into applications.



Any Java class that adheres to certain
conventions regarding property and event
interface definitions can be a JavaBean.



Beans publish their attributes and behaviors
through special method signature patterns that
are recognized by beans
-
aware application
construction tools.


16/03/2013

UFCE4Y
-
20
-
3

7

Example
non
-
visual

Bean

package baked.myBeans;

public class IntHold implements


java.io.Serializable {



private int value;


public IntHold() { value=0; }



public int getValue() { return value; }



public void setValue(int value) {


this.value = value;


}

} //class

16/03/2013

UFCE4Y
-
20
-
3

8

Definition & Convention


Introspection


Properties


Event Handling


Persistence



http://www.oracle.com/us/technologies/java/faq
-
135947.html


16/03/2013

UFCE4Y
-
20
-
3

9

Other Features ...


Packaged


(No parameter constructor)



Introspection


A Bean may be dynamically inspected by an
IDE (eg. BeanBox) and it's characteristics
displayed in a property editor.


Introspection is the automatic process of
analyzing a bean's design patterns to reveal the
bean's properties, events, and methods. This
process controls the publishing and discovery of
bean operations and properties.


Extra information may also be supplied with the Bean

http://stackoverflow.com/questions/2044446/java
-
introspection
-
and
-
reflection


16/03/2013

UFCE4Y
-
20
-
3

10

Properties
-

definition



Properties are public attributes of a Bean
that affect its appearance or behavior


Properties are determined from get/set
access method combinations that follow a
prescribed naming convention


Properties are
simple

or
indexed


Properties may additionally be
bound

or
constrained


16/03/2013

UFCE4Y
-
20
-
3

11

Properties


Accessors and

Mutators




Simple Property


private
type

aa;



public
type

getAa() { return aa; }


public void setAa(
type

aa) { this.aa=aa; }


Indexed Properties


private
type

[ ] aa;



public void setAa(int index,
type

value);


public
type

getAa(int index);



public void setAa(
type

values[ ]);


public
type
[ ] getAa();


16/03/2013

UFCE4Y
-
20
-
3

12

Bound Properties




An object that supports bound properties can
notify other objects when certain property values
change. Such a notification is called a
property
change event
.


http://java.sun.com/developer/onlineTraining/Beans/Beans4/bound.html

16/03/2013

UFCE4Y
-
20
-
3

13

Bound property
-

Events(0)



16/03/2013

UFCE4Y
-
20
-
3

14

Source (Bean)

Target

1. Override
propertyChange( )

2. add a Listener( )

3. Property
changes
-

Event

4. Invoke
propertyChange( )

Properties


Bound
-

Events(1)


Source generates PropertyChangeEvent


private PropertyChangeSupport p;//convenience class


p = new PropertyChangeSupport(this);//in constructor



public void addPropertyChangeListener



(PropertyChangeListener listener) {


p.addPropertyChangeListener (listener);


}




public void removePropertyChangeListener



(PropertyChangeListener listener) {


p.removePropertyChangeListener (listener);


}

16/03/2013

UFCE4Y
-
20
-
3

15

Properties


Bound
-

Events(2)


Source generates PropertyChangeEvent




public void setValue(int value) {




int oldValue = this.value;



this.value = value;




p.firePropertyChange("value",


new Integer(oldValue),


new Integer(value));


}






16/03/2013

UFCE4Y
-
20
-
3

16

Properties


Bound
-
Events(3)


Target receives PropertyChangeEvent



sourceObj.addPropertyChangeListener(


new java.beans.PropertyChangeListener() {




public void propertyChange(




java.beans.PropertyChangeEvent evt) {


// do some processing




this.x = evt.getNewValue();


}


});


NB. Can use

implements PropertyChangeListener

and


sourceObj.addPropertyChangeListener(this)




16/03/2013

UFCE4Y
-
20
-
3

17

Properties


Constrained



Can be vetoed by registered listeners



A veto will prevent the property from
changing



Uses
java.beans.VetoableChangeListener()






16/03/2013

UFCE4Y
-
20
-
3

18

Events



Event Driven Designs



UI


eg. mouse and keyboard events



Inter
-
client connections


eg. Database



Bean property changes

16/03/2013

UFCE4Y
-
20
-
3

19

Persistence



Beans must implement the
Serializable interface



A Bean may be instantiated from a
serialized object. This file has the
same name as the bean with a
.ser

suffix






16/03/2013

UFCE4Y
-
20
-
3

20

Constructor and package


A Bean must provide a default (no
parameter) constructor



A Bean must belong in a package

16/03/2013

UFCE4Y
-
20
-
3

21

Netbeans generated Bean (1)

package delme22;

import java.beans.*;

import java.io.Serializable;


public class MyBean implements Serializable {




private String sample;


private PropertyChangeSupport propertySupport;




public void addPropertyChangeListener(


PropertyChangeListener listener) {


propertySupport.addPropertyChangeListener(listener); }




public void removePropertyChangeListener(


PropertyChangeListener listener) {


propertySupport.removePropertyChangeListener(listener); }




16/03/2013

UFCE4Y
-
20
-
3

22

Netbeans generated Bean (2)





public MyBean() {


propertySupport = new PropertyChangeSupport(this);


}



public String getSample () { return sample; }




public void setSample (String value) {


String oldValue = sample;


sample = value;



propertySupport.firePropertyChange("aSample",


oldValue, sample);


}



} //class

16/03/2013

UFCE4Y
-
20
-
3

23

BeanInfo


The IDE must be able to instantiate
the Bean and discover its properties
for inclusion in the property sheet


JavaBeans framework use an
Introspector

class


A complementary Bean configuration
class optionally accompanies each
Bean


This class has
"BeanInfo"

appended to
the Bean's class name

16/03/2013

UFCE4Y
-
20
-
3

24

Use of BeanInfo



Expose only those features you want to expose.


Rely on
BeanInfo

to expose some Bean features
while relying on low
-
level reflection to expose
others.


Associate an icon with the target Bean.


Segregate features into normal and expert
categories.


Provide a more descriptive display name, or
additional information about a Bean feature.


16/03/2013

UFCE4Y
-
20
-
3

25

Packaging and Distribution


Beans are distributed in
JAR

files



The jar file contains the Bean class plus
any auxilary files (BeanInfo, gifs etc)



Each jar file has a manifest file


the
syntax of this is tricky!

(Deitel's
"Advanced Java 2"

pg 340 has a good discussion on this)


16/03/2013

UFCE4Y
-
20
-
3

26

Some
Pojo

References


http://mindprod.com/jgloss/pojo.html


http://www.roseindia.net/ejb/introduction
-
to
-
pojo.shtml


http://java.sys
-
con.com/node/180374




16/03/2013

UFCE4Y
-
20
-
3

27

Summary



Beans demonstrate the value of
components



Beans are useful and popular because
they provide a consistent interface



Beans are conveniently manipulated in
IDEs



16/03/2013

UFCE4Y
-
20
-
3

28

Next Steps


Examples from:


Practical classes


The WEB



See links on module web site e.g.

developer.java.sun.com/developer/onlineTraining/Beans/JBeansAPI/shortcourse.html


16/03/2013

UFCE4Y
-
20
-
3

29