InjectionPage edited by Bob Harner
Comment:
Structural reorganizing for clarity and consistency
Changes (27)
Full ContentInjectionInjection is Tapestry's way of making a dependency – such as a resource, asset, component, block or service – available in a page, component, mixin or service class. Injection is a key concept in Tapestry, and it is used in several different but related ways. Injection in Tapestry IOC ServicesMain Article: Tapestry IoC Overview The Tapestry IoC container makes use of injection primarily through constructors and via parameters to service builder methods. Injection in Component ClassesFor components, however, Tapestry takes a completely different tack: injection directly into component fields. The @Inject annotation is used to identify fields that will contain injected services and other resources. Tapestry allows for two kinds of injection:
In both cases, the field is transformed into a read only value. As elsewhere in Tapestry, this transformation occurs at runtime (which is very important in terms of being able to test your components). Attempting to update an injected field will result in a runtime exception. In addition, there are a few special cases that are triggered by specific field types, or additional annotations, in addition, to @Inject, on a field. Block InjectionFor field type Block, the value of the Inject annotation is the id of the <t:block> element within the component's template. Normally, the id of the block is determined from the field name (after stripping out any leading "_" and "$" characters):
@Inject
private Block foo;
Asset InjectionMain Article: Assets When the @Path annotation is also present, then the injected value (relative to the component) will be a localized asset. @Inject @Path("context:images/top_banner.png") private Asset banner; Symbols in the annotation value are expanded. Service InjectionHere, a custom EmployeeService service is injected, but any custom or built-in service may be injected in the same way.
@Inject
private EmployeeService employeeService;
A large number of services are provided by Tapestry. See the following packages: Explicit Service InjectionHere, a specific object is requested. A @Service annotation is used to identify the service name. @Inject @Service("Request") private Request request; This is generally not necessary; you should always be able to identify the service to be injected just by type, not by explicit id. Explicit ids have the disadvantage of not being refactoring-safe: this won't happen with the Request service, but perhaps in your own code ... if you rename the service interface and rename the service id to match, your existing injections using the explicit service id will break. Default InjectionWhen the type and/or other annotations are not sufficient to identify the object or service to inject, Tapestry falls back on two remaining steps. It assumes that the field type will be used to identify a service, by the service interface. First, the object provider created by the Alias service is consulted. This object provider is used to disambiguate injections when there is more than one service that implements the same service interface. Second, a search for a unique service that implements the interface occurs. This will fail if either there are no services that implement the interface, or there is more than one. In the latter case, you must disambiguate, either with a contribution to the Alias service, or by explicitly identifying the service with the @Service annotation. Defining New Injection LogicAnonymous injection is controlled by the InjectionProvider service. The configuration for this service is a chain of command for handling component injections.
Change Notification Preferences
View Online
|
View Changes
|
- [CONF] Apache Tapestry > Injection confluence
- [CONF] Apache Tapestry > Injection Bob Harner (Confluence)
