...
For integrating Spring Security into your application, see Security.
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>
|
...
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>
|
...
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;
|
...
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.
Injecting Tapestry services in Spring beans
since |
|
h3. Injecting Tapestry services in Spring beans
If you have configured Spring to allow annotation-based injection, then you will be able to inject Tapestry services into your Spring Beans.
This feature is only available when Spring ApplicationContext is not configured and loaded externally.
Inside your Spring beans, you may use @[Inject|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/ioc/annotations/Inject.html] and @[Autowired|http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/annotation/Autowired.html] annotations.
|
Simply add these two annotations on top the field you want to inject in your Spring bean.
...
...
@Inject
@Autowired
private MyService myService;
...
...
or use @Inject on top of arguments in @Autowired bean constructor methods
...
...
private final MyService myService;
@Autowired
public UserDAOImpl(@Inject MyService myService)
{
this.myService = myService;
}
...
...
h3. Configuring Spring with Tapestry Symbols
This is accomplished by a BeanFactoryPostProcessors that resolves the values of 'placeholders' from symbol values. In the following example the value of the Bean's property 'productionMode' is the value of the Tapestry's symbol [tapestry.production-mode|http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/SymbolConstants.html#PRODUCTION_MODE]
{code:xml}
<bean id="myBean" class="org.example.MyBean">
<property name="productionMode" value="${tapestry.production-mode}"/>
</bean>
{code}
...
ApplicationContextCustomizer
...