Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

Injection 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.

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

Injection is a key concept in Tapestry, and it is used in several different but related ways, listed below.

Injection in Tapestry IOC Services

Main Article: Tapestry IoC Overview Injection in Detail

The Tapestry IoC container makes use of injection primarily through constructors and via parameters to service builder methods.

...

For 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):

Code Block
java
java
@Inject
private Block foo;

Where that is not appropriate, an @Id annotation can be supplied:

Code Block
@Inject
@Id("bar")
private Block barBlock;

The first injection will inject the Block with id "foo" (as always, case is ignored). The second injection will inject the Block with id "bar".

...

A very common example occurs when a component needs access to its resources. The component can define a field of the appropriate type and use the @Inject annotation without a value:

Code Block
java
java
@Inject
private ComponentResources resources;

Tapestry uses the type of the field, ComponentResources, to determine what to inject into this field.

...

When the @Path annotation is also present, then the injected value (relative to the component) will be a localized asset.

Code Block
java
java
@Inject
@Path("context:images/top_banner.png")
private Asset banner;

Symbols in the annotation value are expanded.

...

Here, a custom EmployeeService service is injected, but any custom or built-in service may be injected in the same way.

Code Block
java
java
@Inject
private EmployeeService employeeService;

A large number of services are provided by Tapestry. See the following packages:

Wiki Markup
{float:left|width=15em}
* [Core Services|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/package-summary.html]
* [AJAX Services|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/ajax/package-summary.html]
* [Assets Services|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/assets/package-summary.html]
* [Dynamic Component Services|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/dynamic/package-summary.html]
{float}
Wiki Markup
{float:left|width=15em}
* [JavaScript Services|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/javascript/package-summary.html]
* [Link Transformation Services|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/linktransform/package-summary.html]
* [Message Services|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/messages/package-summary.html]
* [Component Metadata Services|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/meta/package-summary.html]
{float}
Wiki Markup
{float:left|width=15em}
* [Page Loading Services|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/pageload/package-summary.html]
* [Security Services|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/security/package-summary.html]
* [Template Services|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/templates/package-summary.html]
* [Class Transformation Services|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/transform/package-summary.html]
{float}
Wiki Markup
{float:left|width=15em}
* [Tapestry IOC Services|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/services/package-summary.html]
* [Tapestry IOC Cron Services|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/services/cron/package-summary.html]
* [Kaptcha Services|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/kaptcha/services/package-summary.html]
* [File Upload Services|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/upload/services/package-summary.html]
{float}
INLINE
HTML
<div style="clear:both"></div>
 

Explicit Service Injection

Here, a specific object is requested. A @Service annotation is used to identify the service name.

Code Block
java
java
@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.

...