1 / 29

Lecture # 19 Java Beans

SWE 316: Software Design and Architecture. Lecture # 19 Java Beans. Ch 11. Understand what JavaBeans (“Beans”) are? the life-cycle of a Bean Bean containers Be able to create JavaBeans connect Beans in BeanBox create applications that use Beans.

erica
Télécharger la présentation

Lecture # 19 Java Beans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SWE 316: Software Design and Architecture Lecture # 19Java Beans Ch 11 • Understand • what JavaBeans (“Beans”) are? • the life-cycle of a Bean • Bean containers • Be able to • create JavaBeans • connect Beans in BeanBox • create applications that use Beans Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

  2. 2/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Java Beans Design Goals 1 • Create a component technology within Java • capitalize on Java portability • Include GUI components • but not limited to GUI (e.g. server bean) • Compete with other visual programming and component systems • (which are often specific to an O.S.) • usually Windows • require installation of some kind

  3. 3/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Beans Design Goals 2 KEY CONCEPT Design Goal: Reusability • “Light weight” for Internet applications • Secure • use Java security model • Easy & efficient to distribute • Provide mechanism which enables development environment (“container”) to determine methods, properties & events Facilitate the easy reuse of Java code.

  4. 4/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Bean Phases Phase 1. Create Bean Classes Source subject to rules Design / implementation time. Phase 2. Create Bean from Multiple Bean Classes Combine Bean classes to make new Beans; create manifest; compile Instance creation time. Phase 3. Create Bean Instance Instantiate object(s), usually in a Bean environment (container) -------- Assembly time. Phase 4a. Combine Beans in Bean Container to Make Application Combine with other Beans to produce application - or - Deployment time. Phase 4b. Deploy Bean and Use in Applications Place application, Beans and required software on target platform

  5. 5/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Amenities Afforded by Bean Environments • Detection of the Bean’s properties Read only – or- Writeable • Detection of listeners supported So events on the Bean can be handled • Ability to easily create instances and display an image if an awt or swing object Set property values visually • Ability to store instances

  6. 6/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Required Bean Rules 1 of 2 • Java source consists of Java classes • containing null constructor … MyClass() { … } • implementing Serializable interface • obeying standards shown below for … … accessor methods … Listener registration … Event classes

  7. 7/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Required Bean Rules 2 of 2 • To have property myProp, include methods: <type> getMyProp(){ … } // to access myProp void setMyProp( <type> p ) // to change • Forbooleanproperty: booleanisMyProp() • Name for event classes to be XXXEvent • extends Event • Listeners must implement java.util.EventListener • Name must end in Listener as in XXXListener • added withpublic voidaddXXXListener(...) • removed withpublic voidremoveXXXListener(...)

  8. 8/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Phase 1: creating bean classes (cont...) • Compile the bean: javac Bean0.java • Create manifest (text file): specifies the name of the class file comprising the bean and indicates that they are indeed JavaBeans (other required files can also be included). • The manifest file becomes part of the JAR file 11.3 Name: Bean0.class Java-Bean: True

  9. 9/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Bean0 source code public class Bean0 implements java.io.Serializable // required for Beans { private intmyInt = 0; // ("myInt" is not necessarily a property yet!) public Bean0() // Null constructor presence required for all Beans { } public intgetIntgr() // Introduces property "intgr" and makes gettable { return myInt; } public void setIntgr( intanInteger ) // Makes property "intgr" settable { myInt = anInteger; } }

  10. 10/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP JAR’ing a Bean jar cfm Bean0.jar manifest.txt Bean0.class List all .class files to be included Creating a JAR file Second argument is name of manifest file First argument is name of the new JAR file

  11. 11/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP BeanBox Environment

  12. 12/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Adding a Bean to the BeanBox

  13. 13/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Bean1

  14. 14/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Bean1 source code import java.awt.*; public class Bean1 extends Canvas implements java.io.Serializable { private Color color = Color.red; // color of interior rectangle // Set background (color) and size // properties public Bean1() { setBackground( Color.green ); setSize( 80,40 ); } // Establish property "color" public Color getColor() { return color; } public void setColor( Color aColor ) { color = aColor; } // Override paint: rectangle and // message within this Canvas object // Called initially and when developer // changes a property in BeanBox public void paint ( Graphics g ) { // Draw rectangle in "color" g.setColor( color ); // Starting from top left within // this: 20 pixels across, 5 down // draw a filled rectangle 20 // across and 30 down g.fillRect( 30,5,20,30 ); // Write "HELLO WORLD" in the // foreground color g.setColor( getForeground() ); g.drawString( "HELLO WORLD", 5, 20 ); } }

  15. 15/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Setting Color

  16. 16/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Phase 2: creating multiple-class beans • initial Form of Properties Panel • To create a bean consisting of more than one class: • Create these classes in conformance with Bean rules • Declare them in the manifest file • Jar them all • E.g.: jar cfm MyBean.jar manifestListingAll.txt MyBean1.class MyBean2.class … MyBeanN.class 11.4

  17. 17/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Phase 3: creating bean instance • BeanBox helps the user to create instances of Bean classes by allowing the user to set some properties visually. 11.5 (green in color)

  18. 18/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Phase 4: combining and deploying beans • Phase 4a: combining Beans in Bean environment • Beginning to Use ChairMaker

  19. 19/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Phase 4: combining and deploying beans • Phase 4a: combining Beans in Bean environment • Setting ChairMaker to Add a Chair Leg

  20. 20/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Phase 4: combining and deploying beans • Phase 4a: combining Beans in Bean environment, • An Output Of ChairMaker Bean

  21. 21/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Phase 4: combining and deploying beans • Phase 4a: combining Beans in Bean environment • Output Of ChairMaker Bean From Button Action

  22. 22/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Phase 4: combining and deploying beans KEY CONCEPT Reusability • Phase 4b: using Beans in applications: Output We want to associate Beans even when there is no external event such as a mouse click. Object bean1 = Beans.instantiate(null, “Bean1”); Bean1 bean2 = (Bean1)objectInputStream.readObject();

  23. 23/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Connecting Beans via property changes: “bound” variables 11.7 Property Change Event Demonstration

  24. 24/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Connecting Beans via property changes: “bound” variables • Property Change Event Demonstration (cont...)

  25. 25/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Connecting beans via property changes: “bound” variables KEY CONCEPT Bound Properties • bound property demonstration -- causes a Beans to react when a property in another Bean changes value.

  26. 26/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Embedding Beans in JSP <jsp:useBean id="object name" 1 scope="page|request|session|application" 2 class="fully qualified classname" 3 </ jsp:useBean > • 1 Bean instance name as in MyClassmyName = …. • 2 // Choose one; when instance is destroyed; optional; default is page • 3 // e.g., a.b.MyClass 11.8

  27. 27/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Scope of a Bean in a JSP • page - new object created and destroyed for every page view. • request - the newly created object created and bound to the request object. • session - the newly created object bound to the session object. -- every visitor coming to the site will have a separate session for it, so you will not have to create a new object every time for it -- can retrieve that object later again from the session object when wanted • application - object will stay as long as the application remains loaded. E.g., you want to count page views or daily sessions for your site. Source: http://stardeveloper.com:8080/articles/072001-1.shtml

  28. 28/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Setting and Getting a Bean Property in a JSP: Introduction • <jsp:setProperty • name=“account17" • property=“bal" • value=“3211“ • /> • <jsp:getProperty • name=“account17" • property=“bal" • />

  29. 29/29 Intro Creating Bean Classes Multiple-Class Beans Deploying Beans Connecting Beans Beans in JSP Summary of This Chapter • A Java Bean is a compiled collection of Java classes and required files • JAR’d to reduce to a single file • Beans are used at various phases, often in a Bean container • Creating from scratch • Creating instances of • Connecting • Deploying as part of an application

More Related