Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Fixed bad links due to copy-paste from cwiki-test

Scrollbar
Excerpt
hiddentrue

a guide to what goes in your application module (usually AppModule.java)

 

...

The Application Module class is a plain simple Java class used to configure Tapestry. A system of annotations and naming conventions allows Tapestry to determine what services are provided by the module to your application. This is the place where you bind your custom implementation of services, contribute to, decorate and override existing services.

Table of Contents

Div
stylefloat:right
titleRelated Articles
classaui-label
Content by Label
showLabelsfalse
showSpacefalse
titleRelated Articles
cqllabel = "configuration" and space = currentSpace()

For complete documentation, you should refer to the IOC Service guideline.

...

Note

Allowing for non-static methods may have been a design error, a kind of premature optimization. The thinking was that the module could have common dependencies that it could then easily access when building services. This was partly about runtime efficiency but mostly about reducing redundancy in the various service building, contribution, and decorating methods; the ServieBinder ServiceBinder came later, and was a better solution (trading runtime efficiency for developer ease of use).

...

Every module may have an optional, static bind() method which is passed a ServiceBinder. By using the ServiceBinder, you will let Tapestry autobuild your services. Autobuilding is the preferred way to instantiate your services.

Code Block

package org.example.myapp.services;

import org.apache.tapestry5.ioc.ServiceBinder;

public class MyAppModule
{
  public static void bind(ServiceBinder binder)
  {
    binder.bind(Indexer.class, IndexerImpl.class);
  }
}

...

Sometime you need to do more than just instantiate the class with dependencies. It is common inside Tapestry for one service to be a listener to events from another service. In that situation (or other similar ones), a service builder method is useful, as it shifts control back to your code, where you have the freedom to perform any additional operations necessary to get the service implementation up and running.

Code Block

package org.example.myapp.services;

public class MyAppModule
{
  public static Indexer build()
  {
    return new IndexerImpl();
  }
}

...

Here's a more complicated example:

Code Block

    @Marker(ClasspathProvider.class)
    public static AssetFactory buildClasspathAssetFactory(ResourceCache resourceCache,
            ClasspathAssetAliasManager aliasManager, AssetPathConverter converter)
    {
        ClasspathAssetFactory factory = new ClasspathAssetFactory(resourceCache, aliasManager, converter);

        resourceCache.addInvalidationListener(factory);

        return factory;
    }

...

Tapestry has evolved some additional tools to "have your cake and eat it too"; the @Autobuild @Autobuild annotation takes care of instantiating a service implementation, with dependencies, allowing your code to focus on the extra initialization logic, and not on the dependencies:

Code Block

    public static PersistentFieldStrategy buildClientPersistentFieldStrategy(LinkCreationHub linkCreationHub, @Autobuild
    ClientPersistentFieldStrategy service)
    {
        linkCreationHub.addListener(service);

        return service;
    }

...

Services will be annotated with @UsesConfiguration@UsesConfiguration.

For example, here's a kind of tapestry internal service that requires a list of Coercion tuples to be able to coerce values from one type to another (i.e. from string to the target type when reading values from the Http HTTP request)

Code Block

public TypeCoercerImpl(Collection<CoercionTuple> tuples)
{
    // ...
}

On the contribution side, a service contribution method sees a Configuration object:

Code Block

public static void contributeTypeCoercer(Configuration<CoercionTuple> configuration) {
{
    // Create Coercion tuple here
    // ...

    configuration.add(myTuple);
}

Decorate methods

content under development

Annotations

Main Article: Annotations

Tapestry versions 5.2 comes and later come with a set of of annotation to better your understanding of annotations that are commonly used in module classes.

See this list of IOC annotations

Anchor
parameter-types
parameter-types
Parameter types

...

Configuration parameter types

content under development

Link to services

content under development

Symbols

Main Article: Symbols

content under development

Load services on registry startup

content under development

Define service scope

content under development

Disambiguate services

content under development

With service Id

content under development

With Markers

content under development

Override existing services

content under development

 

Scrollbar