Sunday, July 27, 2008

@Marker annotation of tapestry5

Used to define one or more marker annotations for a service implementation. This allows for injection based on the combination of type and marker interface. These marker interfaces should not have any values. The mere presence of the marker annotation is all that is needed.
当一个interface有多个实现的service时,可以为每个service实现定义一个标记(@Marker),然后就可结合对象接口的类型和这个标记来确认service并注入此service实现,当然也可以用@Inject结合@Service("serviceID")这种方式注入。

Tapestry defines two such services, in the TapestryModule.


@Marker(ClasspathProvider.class)
public AssetFactory buildClasspathAssetFactory(ResourceCache resourceCache,

ClasspathAssetAliasManager aliasManager)
{
ClasspathAssetFactory factory = new ClasspathAssetFactory(resourceCache, aliasManager);

resourceCache.addInvalidationListener(factory);

return factory;
}

@Marker(ContextProvider.class)
public AssetFactory buildContextAssetFactory(ApplicationGlobals globals)
{
return new ContextAssetFactory(request, globals.getContext());
}

Here's an example, you can see how Tapestry is figuring out which service to inject based on the presence of those annotations:

public void contributeAssetSource(MappedConfiguration configuration,
@ContextProvider
AssetFactory contextAssetFactory,

@ClasspathProvider
AssetFactory classpathAssetFactory)
{
configuration.add("context", contextAssetFactory);
configuration.add("classpath", classpathAssetFactory);
}

Reference: http://tapestry.apache.org/tapestry5/tapestry-ioc/cookbook/basics.html

No comments :