Show Table of Contents
1.20. Configuring Bean Contexts
- To have the contents of the bean context returned at the end of a
Smooks.filterSourceprocess, supply aorg.milyn.delivery.java.JavaResultobject in the call to theSmooks.filterSourcemethod.Example 1.1.
// Get the data to filter StreamSource source = new StreamSource(getClass().getResourceAsStream("data.xml")); // Create a Smooks instance (cachable) Smooks smooks = new Smooks("smooks-config.xml"); // Create the JavaResult, which will contain the filter result after filtering JavaResult result = new JavaResult(); // Filter the data from the source, putting the result into the JavaResult smooks.filterSource(source, result); // Getting the Order bean which was created by the Javabean cartridge Order order = (Order)result.getBean("order"); - To access the bean contexts at start-up, specify this in the
BeanContextobject. You can retrieve it from theExecutionContextvia thegetBeanContext()method.Example 1.2.
// Create a bean to pass on to the Smooks execution context HashMap<String,Object> transformConfig = new HashMap<String,Object>(); transformConfig.put("Version", new Integer(1)); // Get the data to filter StreamSource source = new StreamSource(getClass().getResourceAsStream("data.xml")); // Create a Smooks instance (cachable) Smooks smooks = new Smooks("smooks-config.xml"); // Create the JavaResult, which will contain the filter result after filtering JavaResult result = new JavaResult(); // Add bean to Smooks execution context executionContext.getBeanContext().addBean("transformConfig", transformConfig); // Filter the data from the source, putting the result into the JavaResult smooks.filterSource(source, result); // Getting the Order bean which was created by the JavaBean cartridge Order order = (Order)result.getBean("order"); - When adding or retrieving objects from the
BeanContextmake sure you first retrieve abeanIdobject from thebeanIdStore. ThebeanIdobject is a special key that ensures higher performance than string keys, although string keys are also supported. - You must retrieve the
beanIdStorefrom theApplicationContextusing thegetbeanIdStore()method. - To create a
beanIdobject, call theregister("beanId name")method. If you know that the beanId is already registered, then you can retrieve it by calling thegetbeanId("beanId name")method. beanIdobjects areApplicationContext-scoped objects. Register them in your custom visitor implementation's initialization method and then put them in the visitor object as properties. You can then use them in thevisitBeforeandvisitAftermethods. ThebeanIdobjects and thebeanIdStoreare thread-safe.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.