Tapestry easily integrates with Spring Framework, allowing beans defined by Spring to be injected into Tapestry IoC services, and into Tapestry components. In addition, with Tapestry 5.2 and later, you can also go the other way, injecting Tapestry services in Spring beans.
Div | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
|
For integrating Spring Security into your application, see Security Integrating with Spring Framework.
Contents
Table of Contents | ||||
---|---|---|---|---|
|
Spring Version
This module is compiled and tested against Spring Framework 2.5.6. It should be reasonable to override the dependency to earlier versions of Spring, though the code makes use of some APIs that were added to Spring to support JDK 1.5 annotations.
...
To integrate Spring with Tapestry, you should add the below dependency in your classpath. The following exemple is for Maven users.
Code Block | ||
---|---|---|
| ||
<dependency> <groupId>org.apache.tapestry</groupId> <artifactId>tapestry-spring</artifactId> <version>[your-tapestry-version]</version> </dependency> |
Update your web.xml file
The short form is that you must make two small changes to your application's web.xml.
First, a special filter is used in replace of the standard TapestryFilter:
Code Block | ||
---|---|---|
| ||
<filter> <filter-name>app</filter-name> <!-- Special filter that adds in a T5 IoC module derived from the Spring WebApplicationContext. --> <filter-class>org.apache.tapestry5.spring.TapestrySpringFilter</filter-class> </filter> |
Secondly, you may add the normal Spring configuration, consisting of an optional <context-param> identifying which Spring bean configuration file(s) to load:
Code Block | ||
---|---|---|
| ||
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value> </context-param> |
The <context-param> lists the Spring bean configuration file. It is optional and defaults to just /WEB-INF/applicationContext.xml if omitted.
...
By integrating Spring in Tapestry, you get full access on Spring ApplicationContext as if you were accessing to any Tapestry service. Simply @Inject into your pages and components.
Code Block | ||
---|---|---|
| ||
@Inject private ApplicationContext springContext; |
Injecting beans
Inside your component classes, you may use the @Inject annotation. Typically, just adding @Inject to the field type is sufficient to identify the Spring bean to inject:
Code Block | ||
---|---|---|
| ||
@Inject private UserDAO userDAO; |
Searching for Spring beans is threaded into the MasterObjectProvider service. The Spring context becomes one more place that Tapestry searches when determining the injection for a injected field or method parameter.
...
- You may now use the @Inject or @InjectService annotations inside Spring beans; these will be resolved to Tapestry services or other objects available via the MasterObjectProvider. Please see the detailed guide to Injection.
- The dependency on Spring is no longer scope "provider" and has changed to 2.5.6.
- Spring Beans are no longer exposed as services, unless 5.0 compatibility mode is enabled.
- You no longer create a ContextLoaderListener.
...