home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book Home Java Enterprise in a Nutshell Search this book

6.3. Bean Contexts and Services

The JavaBeans component model was introduced in Java 1.1. Java 1.2 extends that model by introducing a containment and services protocol, defined in the java.beans.beancontext package. A bean context is a java.util.Collection of beans that implements the BeanContext interface and provides a context for the beans it contains. Many bean contexts define one or more services, such as a printing service, that beans can query and use. These bean contexts implement the BeanContextServices interface. All bean contexts are also BeanContextChild implementations, so contexts can be nested within each other.

Many beans never need to know about the contexts that contain them. A bean that does want to take advantage of its context and the services it provides implements the BeanContextChild interface. When a bean context child is added to a bean context, the setBeanContext() method of the BeanContextChild interface is invoked by the bean context. The implementation of this method should store the reference to the bean context for future use. The setBeanContext() method is a bound and constrained property, so it must notify VetoableChangeListener and PropertyChangeListener objects. For this reason, many beans delegate these responsibilities to a BeanContextChildSuport object.

If a bean (or bean context) is nested within a bean context that implements BeanContextServices, the bean can use the services provided by the bean context. A service is identified by the Java class that defines it. So a printing service is identified by the Class object of the java.awt.print.PrinterJob class, for example, and the system clipboard service is represented by the java.awt.datatransfer.Clipboard class. A bean can call the hasService() method of its containing BeanContextServices object to determine whether a specified service is available. If so, it can use getService() to obtain an appropriate instance of the service class. If a bean context is nested within another context, it can pass these hasService() and getService() methods to its containing context.

In addition to getService() and hasService(), a BeanContext provides several other methods beans can rely on. getResource() and getResourceAsStream() replace the methods by the same name defined by Class and ClassLoader. The isDesignTime() method (from the DesignMode interface) allows a bean to determine whether it is being displayed within a beanbox or run in an application or applet. The BeanContext method is preferred to the static Beans.isDesignTime() method because it is context-specific rather than global.

BeanContext and BeanContextServices are large interfaces; implementations must adhere to fairly complex specifications that govern the ways they interact with the beans they contain and the contexts within which they are nested. For these reasons, bean developers do not often create custom bean contexts. Instead, they rely on the contexts provided by the vendor of their beanbox tool. Advanced bean developers who do need to create bean contexts can delegate many of their methods to the BeanContextSupport and BeanContextServicesSupport classes that implement the basic framework and protocols.



Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.











??????????????@Mail.ru