1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
|
<?xml version="1.0" encoding="UTF-8"?>
<chapter id="basics">
<title>Basic Configuration and Usage</title>
<para>The microcontainer's main purpose is to allow external configuration of POJOs. As we have seen in <xref linkend="examples"/>, the POJOs in a microcontainer applications are nothing special. The key element that drives the application is the <varname>jboss-beans.xml</varname> configuration file. So, in this chapter, we will look at the some of the common configurations in <varname>jboss-beans.xml</varname>.</para>
<section>
<title>Deployment</title>
<para>The <varname>deployment</varname> element acts as a container for many beans that are deployed together.</para>
<programlisting><?xml version="1.0" encoding="UTF-8"?>
<!-- Deployment holds beans -->
<deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:jboss:bean-deployer bean-deployer_1_0.xsd"
xmlns="urn:jboss:bean-deployer">
<!-- bean part of the deployment -->
<bean .../>
<!-- bean part of the deployment -->
<bean .../>
</deployment></programlisting>
</section>
<section>
<title>Bean</title>
<para>The <varname>bean</varname> element is the main deployment component. It describes a single managed object in the runtime.</para>
<programlisting>
<deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:jboss:bean-deployer bean-deployer_1_0.xsd"
xmlns="urn:jboss:bean-deployer">
<bean name="Simple"
class="org.jboss.example.microcontainer.simple.SimpleBean"/>
</deployment>
</programlisting>
<para>The example above from the <varname>simple</varname> example uses the default constructor of <varname>SimpleBean</varname> to create a new POJO.</para>
<programlisting>
new org.jboss.example.microcontainer.simple.SimpleBean();
</programlisting>
<para>It is given the name <varname>"Simple"</varname> such that it can be referenced elsewhere.</para>
</section>
<section>
<title>Construction</title>
<para>The example above uses the default constructor. What if you don't want to use some other constructor. The simplest mechanism just matches the number of parameters in the constructor. The example below is from the <varname>constructor</varname> example.</para>
<programlisting>
public class ConstructorBean
{
... ...
public ConstructorBean(int integer)
{
... ...
}
</programlisting>
<para>The <varname>jboss-beans.xml</varname> element for creating the POJO using the above constructor is as follows.</para>
<programlisting>
<bean name="Integer"
class="org.jboss.example.microcontainer.constructor.ConstructorBean">
<constructor>
<parameter>4</parameter>
</constructor>
</bean>
</programlisting>
<para>The microcontainer would just use the following to create the <varname>Integer</varname> named component.</para>
<programlisting>
new ConstructorBean (4);
</programlisting>
<para>Sometimes there are two constructors with the same number of parameters. In this case, you must specify the types to resolve the ambiguity.</para>
<programlisting>
public class ConstructorBean {
public ConstructorBean(String string, int integer) {}
public ConstructorBean(String string, long long) {}
}
</programlisting>
<para>The following configuration creates a managed bean instance named <varname>StringLong</varname> using the second constructor listed above.</para>
<programlisting>
<bean name="StringLong"
class="org.jboss.example.microcontainer.constructor.ConstructorBean">
<constructor>
<parameter>a string</parameter>
<parameter class="long">10</parameter>
</constructor>
</bean>
</programlisting>
<para>Behind the scene, the microcontainer invokes:</para>
<programlisting>
new ConstructorBean (new String("a string"), (long) 10);
</programlisting>
<para>Note that you only have to be explicit enough to resolve the ambiguity.</para>
</section>
<section>
<title>Factories</title>
<para>Not all classes have public constructors. It is often good practice to use factories when you to have a choice of implementation for an interface. The microcontainer includes support for the several different types of factory. The simplest case is a static factory class with a static factory method like the following from the <varname>factory</varname> example.</para>
<programlisting>
public class StaticFactory
{
public static FactoryCreatedBean createBean()
{
return new FactoryCreatedBean("StaticFactory.createBean()");
}
}
</programlisting>
<para>The bean configuration tells the microcontainer to the use the <varname>StaticFactory.createBean()</varname> static method to create an instance of <varname>FactoryCreatedBean</varname>.</para>
<programlisting>
<bean name="StaticFactoryCreatedBean"
class="org.jboss.example.microcontainer.factory.FactoryCreatedBean">
<constructor factoryMethod="createBean"
factoryClass="org.jboss.example.microcontainer.factory.StaticFactory"/>
</bean>
</programlisting>
<para>Of course, the factory class itself does not have to be static. The microcontainer can create a non-static factory itself as a managed object, and then use this factory object to create other bean objects. For instance, the <varname>factory</varname> example contains a singleton factory class example.</para>
<programlisting>
public class SingletonFactory
{
private static SingletonFactory singleton;
public synchronized static SingletonFactory getInstance()
{
if (singleton == null)
singleton = new SingletonFactory();
return singleton;
}
public FactoryCreatedBean createBean()
{
return new FactoryCreatedBean("SingletonFactory.createBean()");
}
private SingletonFactory()
{
System.out.println("SingletonFactory()");
}
}
</programlisting>
<para>In the following configuration, we first create an instance of the <varname>SingletonFactory</varname> class under the name <varname>"SingletonFactory"</varname> using the <varname>SingletonFactory.getInstance()</varname> static method. Then, we use this factory object to create a <varname>FactoryCreatedBean</varname> instance under the name <varname>SingletonFactoryCreatedBean</varname>.</para>
<programlisting>
<bean name="SingletonFactory"
class="org.jboss.example.microcontainer.factory.SingletonFactory">
<constructor factoryMethod="getInstance"
factoryClass="org.jboss.example.microcontainer.factory.SingletonFactory"/>
</bean>
<bean name="SingletonFactoryCreatedBean"
class="org.jboss.example.microcontainer.factory.FactoryCreatedBean">
<constructor factoryMethod="createBean">
<factory bean="SingletonFactory"/>
</constructor>
</bean>
</programlisting>
<para>Like the constructor method we mentioned before, the factory method can also take call parameters via the nested <varname>parameter</varname> element.</para>
</section>
<section>
<title>Properties</title>
<para>It is possible to create all objects using factories and constructors, however many people use the JavaBean or MBean convention where an object is constructed using a default constructor and then configured using properties or attributes (i.e., setter methods). The following class from the <varname>properties</varname> example is a JavaBean style POJO.</para>
<programlisting>
public class PropertiesBean
{
public PropertiesBean()
{
System.out.println("PropertiesBean()");
}
public void setTitle(String title)
{
System.out.println("setTitle: " + title);
}
public void setSubTitle(String subTitle)
{
System.out.println("setSubTitle: " + subTitle);
}
public void setLink(URL url)
{
System.out.println("setLink: " + url);
}
public void setNumber(Number number)
{
System.out.println("setNumber: " + number + " type=" + number.getClass().getName());
}
}
</programlisting>
<para>The configuration creates the <varname>PropertiesBean</varname> instance and then use the setter methods to set its properties.</para>
<programlisting>
<bean name="PropertiesBean"
class="org.jboss.example.microcontainer.properties.PropertiesBean">
<property name="title">JBoss Microcontainer property example</property>
<property name="subTitle"><null/></property>
<property name="link">http://www.jboss.org</property>
<property name="number" class="java.lang.Long">4</property>
</bean>
</programlisting>
<para>Here we use the string representation of various objects, including the <varname>null</varname> value. They will be discussed in more detail in the next section.</para>
</section>
<section>
<title>String Values</title>
<para>Now, you probably noticed that we use string values to represent all kinds of objects in the <varname>parameter</varname> element for the constructors or factory methods, and in the <varname>property</varname> element for JavaBean properties.</para>
<para>In the most generic case, a JavaBean <ulink url="http://java.sun.com/j2se/1.4.2/docs/api/java/beans/PropertyEditor.html">PropertyEditor</ulink> can be used to convert a string to a specific type. JBoss already provides a large number of property editors for standard types. Please see the example below for the POJO class, the configuration, and the resultant microcontainer action.</para>
<programlisting>
import java.beans.PropertyEditorSupport;
public class URLEditor extends PropertyEditorSupport{
public void setAsText(final String text){
setValue(new URL(text));
}
}
public class Example{
public URL getLink() {}
public void setLink(URL url) {}
}
<bean name="Name1" class=com.acme.Example">
<property name="link">http://acme.com/index.html</property>
</bean>
Example example = new com.acme.Example();
PropertyEditor editor = PropertyEditorManager.findEditor(URL.class);
editor.setAsText("http://acme.com/index.html");
example.setLink((URL)editor.getValue());
</programlisting>
<para>Often the property takes an interface or abstract class, but you want to pass a specific implementation or a subclass. That is easy. Just specify the implementation class in the <varname>property.class</varname> attribute as show below.</para>
<programlisting>
public class Example{
public Number getNumber() {}
public void setNumber(Number number) {}
}
<bean name="Name1" class=com.acme.Example">
<property name="number" class="java.lang.Long">4</property>
</bean>
Example example = new com.acme.Example();
example.setNumber(new Long(4));</programlisting>
<para>There is also a more long-winded form of value that we will see later when configuring collections.</para>
<programlisting>
public class Example{
public Number getNumber() {}
public void setNumber(Number number) {}
}
<bean name="Name1" class=com.acme.Example">
<property name="number"><value class="java.lang.Long">4</value></property>
</bean>
Example example = new com.acme.Example();
example.setNumber(new Long(4));
</programlisting>
<para>Finally, the <varname>null</varname> value is trivial, <null/>. But, it needs to differentiated from the string "null". Please see the example below for the usage.</para>
<programlisting>
public class Example{
public String getTitle() {}
public void setTitle(String string) {}
}
<!-- Wrong -->
<bean name="Name1" class=com.acme.Example">
<property name="title">null</property>
</bean>
Example example = new com.acme.Example();
example.setTitle(new String("null"));
<!-- Correct -->
<bean name="Name1" class="com.acme.Example">
<property name="title"><null/></property>
</bean>
Example example = new com.acme.Example();
example.setTitle(null);
</programlisting>
</section>
<section>
<title>Injections</title>
<para>Objects by themself are not very useful. They need to be linked together to form more complicated data structures. We have already seen one form of an injection when using factory instances above. Injections can be used anywhere a string value is used. All the examples that we have previously seen with strings could also be done with injections.</para>
<para>The <varname>injection</varname> example shows how dependency injection works in JBoss Microcontainer. The <varname>InjectionBean</varname> class has a <varname>host</varname> property, which is the <varname>java.net.URL</varname> type. We will inject an URL object into the bean via the microcontainer.</para>
<programlisting>
public class InjectionBean
{
String name;
public InjectionBean(String name)
{
this.name = name;
System.out.println("InjectionBean() " + this);
}
public String toString()
{
return name;
}
public void setHost(String host)
{
System.out.println("setHost: " + host + " on " + this);
}
public void setOther(InjectionBean other)
{
System.out.println("setOther: " + other + " on " + this);
}
}
</programlisting>
<para>The microcontainer creates the <varname>URL</varname> object first, and then passes the <varname>URL</varname> object as a property into the <varname>InjectionBean1</varname> object when it is instantiated.</para>
<programlisting>
<bean name="URL" class="java.net.URL">
<constructor>
<parameter>http://www.jboss.org/index.html</parameter>
</constructor>
</bean>
<bean name="InjectionBean1"
class="org.jboss.example.microcontainer.injection.InjectionBean">
<constructor>
<parameter>InjectionBean1</parameter>
</constructor>
<property name="host"><inject bean="URL" property="host"/></property>
</bean>
</programlisting>
<para>The order of the <varname>bean</varname> elements does not matter. The microcontainer orders the beans into the correct order. For instance, in the above example, the <varname>URL</varname> object is always created before the <varname>InjectionBean1</varname> object since the latter is dependent on the former. But that leaves the problem of how to resolve circular dependencies. These can be resolved by specifying when you want the injection to occur. In the example below we say once <varname>Circular1</varname> is "Instantiated" (constructed) it is ok to configure it on <varname>Circular2</varname>. Normally, injections wait for the referenced bean to reach the state "Installed" (see later on life cycles).</para>
<programlisting>
<bean name="Circular1"
class="org.jboss.example.microcontainer.injection.InjectionBean">
<constructor>
<parameter>Circular1</parameter>
</constructor>
<property name="other"><inject bean="Circular2"/></property>
</bean>
<bean name="Circular2"
class="org.jboss.example.microcontainer.injection.InjectionBean">
<constructor>
<parameter>Circular2</parameter>
</constructor>
<property name="other">
<inject bean="Circular1" state="Instantiated"/>
</property>
</bean>
</programlisting>
<para>Here is the order the microcontainer instantiates those objects.</para>
<programlisting>
InjectionBean Circular1 = new InjectionBean ();
InjectionBean Circular2 = new InjectionBean ();
Circular1.setOther(Circular2); // don't wait for a fully configured Circular1
Circular2.setOther(Circular1); // Complete the confguration of Circular2
</programlisting>
</section>
<section>
<title>Collections</title>
<para>The <varname>collection</varname>, <varname>list</varname>, <varname>set</varname> and <varname>array</varname> elements are used to enclose collection of values to pass to the bean components as properties or constructor (factory method) parameters. The default types are:</para>
<itemizedlist mark="bullet">
<listitem><para>collection: java.util.ArrayList</para></listitem>
<listitem><para>list: java.util.ArrayList</para></listitem>
<listitem><para>set: java.util.HashSet</para></listitem>
<listitem><para>array: java.lang.Object[]</para></listitem>
</itemizedlist>
<para>They all take the same form. So, only <varname>list</varname> is shown here in those examples. You just need to nest <varname>value</varname> elements inside the collection element to specify the contents of the collection. Please note that a <varname>"elementClass"</varname> attribute is required on the collection element, unless you specify explicit types on all the <varname>value</varname>s.</para>
<para>Below is a sample configuration from the <varname>collections</varname> example. It sets a <varname>List</varname> with two elements of mixed types to the <varname>ObjectPrinter.print</varname> property on the <varname>PrintList</varname> named object.</para>
<programlisting>
<bean name="PrintList"
class="org.jboss.example.microcontainer.collections.ObjectPrinter">
<constructor><parameter>List</parameter></constructor>
<property name="print">
<list elementClass="java.lang.String">
<value>Value of type elementClass</value>
<value class="java.lang.Integer">4</value>
</list>
</property>
</bean>
</programlisting>
<para>It is also possible to use a <varname>List</varname> as an element inside another <varname>List</varname>. Here is an example.</para>
<programlisting>
<bean name="Name1" class="com.acme.Example">
<property name="list">
<list class="java.util.LinkedList" elementClass="java.lang.String">
<value>A string</value> <!-- uses elementClass -->
<value class="java.lang.URL">http://acme.com/index.html</value> <!-- a URL -->
<value><inject bean="SomeBean"/></value> <!-- inject some other bean -->
<value> <!-- a list inside a list -->
<list elementClass="java.lang.String">
<value>Another string</value>
</list>
</value>
</list>
</property>
</bean>
</programlisting>
<para>Below is what happens inside the microcontainer.</para>
<programlisting>
Example example = new com.acme.Example();
List list = new LinkedList();
list.add(new String("A string"));
list.add(new URL("http://acme.com/index.html"));
list.add(someBean);
List subList = new ArrayList();
subList.add(new String("Another string"));
list.add(subList);
element.setList(list);
</programlisting>
<para>The other type of collection is a map which also covers Properties and Hashtables. The default is <varname>java.util.HashMap</varname>. The <varname>entry</varname> element inside the map differentiates each <varname>key</varname> and <varname>value</varname> pair. For maps there are two default types for the elements: the <varname>keyClass</varname> and <varname>valueClass</varname>. Below is a map sample from the <varname>collections</varname> example.</para>
<programlisting>
<bean name="PrintMap"
class="org.jboss.example.microcontainer.collections.ObjectPrinter">
<constructor><parameter>Map</parameter></constructor>
<property name="print">
<map keyClass="java.lang.String" valueClass="java.lang.String">
<entry>
<key>Key1 of type keyClass</key>
<value>Value1 of type valueClass</value>
</entry>
<entry>
<key>Key2 of type keyClass</key>
<value class="java.lang.Integer">4</value>
</entry>
<entry>
<key class="java.lang.Long">4</key>
<value>Value of type valueClass</value>
</entry>
</map>
</property>
</bean>
</programlisting>
</section>
<section>
<title>Lifecycle</title>
<para>Anybody familiar with the JBoss JMX microkernel will know about the lifecycle. The microcontainer extends the lifecycle concept to the managed POJOs. A POJO can have the following lifecycle states.</para>
<itemizedlist mark="bullet">
<listitem><para>Not Installed: The POJO has not been described or has been uninstalled.</para></listitem>
<listitem><para>Described: The POJO's bean description has been examined and dependencies determined.</para></listitem>
<listitem><para>Instantiated: All the dependencies have been resolved to construct the bean, these include, the class exists, the constructor parameter injections can be resolved, any factory can be resolved.</para></listitem>
<listitem><para>Configured: All the property injections can be resolved, this includes all the dependencies in any collections.</para></listitem>
<listitem><para>Create: All the dependent beans have been "created", this includes any injections passed to the create method.</para></listitem>
<listitem><para>Start: All the dependent beans have been "started", this includes any injections passed to the start method.</para></listitem>
<listitem><para>Installed: The lifecycle is complete.</para></listitem>
<listitem><para>*** ERROR ***: Some unexpected error occured, usually due to misconfiguration.</para></listitem>
</itemizedlist>
<para>At each stage of the lifecycle, the corresponding method in the bean class is automatically called by the microcontainer, so that you can programatically control how the objects behave throughout its lifecycle. For instance, the <varname>start()</varname> method in the bean class is called when the bean enters the <varname>Start</varname> state. Below is the <varname>LifecycleBean</varname> class from the <varname>lifecycle</varname> example.</para>
<programlisting>
public class LifecycleBean
{
String name;
public LifecycleBean(String name)
{
this.name = name;
System.out.println("LifecycleBean() " + this);
}
public void create()
{
System.out.println("create: " + this);
}
public void start()
{
System.out.println("start: " + this);
}
public void stop()
{
System.out.println("stop: " + this);
}
public void destroy()
{
System.out.println("destroy: " + this);
}
public String toString()
{
return name;
}
}
</programlisting>
<para>The <varname>depends</varname> element allows two beans to perform two phase startup processing like the JMX microkernel.</para>
<programlisting>
<bean name="Lifecycle1"
class="org.jboss.example.microcontainer.lifecycle.LifecycleBean">
<constructor>
<parameter>Lifecycle1</parameter>
</constructor>
<depends>Lifecycle2</depends>
</bean>
<bean name="Lifecycle2"
class="org.jboss.example.microcontainer.lifecycle.LifecycleBean">
<constructor>
<parameter>Lifecycle2</parameter>
</constructor>
</bean>
</programlisting>
<para>The microcontainer resolves the dependency and starts both beans in the appropriate order. Below is the console output when you run the <varname>lifecycle</varname> example. It shows when various lifecycle methods are called when the bean enters those states.</para>
<programlisting>
run:
[java] LifecycleBean() Lifecycle1
[java] LifecycleBean() Lifecycle2
[java] create: Lifecycle2
[java] create: Lifecycle1
[java] start: Lifecycle2
[java] start: Lifecycle1
[java] stop: Lifecycle1
[java] stop: Lifecycle2
[java] destroy: Lifecycle1
[java] destroy: Lifecycle2
</programlisting>
<para>The <varname>create()</varname>, <varname>start()</varname>, <varname>stop()</varname> and <varname>destroy()</varname> methods can be overridden with parameters passed to them. Below is an example on how to override the <varname>create()</varname> method via the <varname>jboss-beans.xml</varname> configuration file.</para>
<programlisting>
public class Example{
public void initialize(Object someObject) {}
}
<bean name="Name1" class="com.acme.Example">
<create method="initialize">
<parameter><inject bean="SomeBean"/></parameter>
</create>
</bean>
</programlisting>
</section>
<section>
<title>Installation</title>
<para>As of 2.0.0, you can provide generic install/uninstall actions. Allowing you to dynamically setup repositories. Note the use of <varname>this</varname> to pass yourself as a parameter. If you exlude the bean name on the action, the operation is performed on yourself.</para>
<programlisting>
<bean name="Name1" class="java.util.Timer"/>
<bean name="Name2" ...>
<install bean="Name1" method="schedule">
<parameter><this/></parameter>
<parameter>100</parameter>
<parameter>10000</parameter>
</install>
<uninstall method="cancel"/>
</bean>
// Install
Name1 = new Timer();
Name2 = ...;
Name1.schedule(Name2, 100, 10000);
// Uninstall
name2.cancel();
</programlisting>
</section>
<section>
<title>ClassLoader</title>
<para>The Microcontainer supports configuration of the classloader at either the deployment or bean level. The classloader element has three alternatives.</para>
<programlisting>
// deployment level - applies to all beans in the deployment
<deployment>
<classloader><inject bean="ClassLoaderName"/></classloader>
// bean level
<bean name="Name2" ...>
<classloader><inject bean="ClassLoaderName"/></classloader>
</bean>
// bean level will use any deployment level classloader
<bean name="Name2" ...>
</bean>
// bean level as null to not use any deployment level classloader
<bean name="Name2" ...>
<classloader><null/></classloader>
</bean>
</programlisting>
</section>
</chapter>
|