17. Integration with Spring

17.1. Integrate with Spring IOC

Asta4D can be integrated with any dependency injection mechanism. We will introduce how to integrate with Spring IOC as example.

There is a package called asta4d-spring which supplies reference implementation to show how to integrate a DI container to Asta4D. Firstly, as an alternative for Asta4dServlet introduced in the previous chapter, SpringInitializableServlet can be used to initialize Spring configuration automatically. The web.xml file should be like following:

Example 17.1. 


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- integrate with spring -->
    <servlet>
        <servlet-name>Asta4D Servlet</servlet-name>
        <servlet-class>com.astamuse.asta4d.misc.spring.SpringInitializableServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/configuration.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Asta4D Servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

      

Secondly, the WebApplicationConfiguration instance should be declared as a bean in spring with additional Spring special configurations:

Example 17.2. 


<?xml version="1.0" encoding="UTF-8"?>
    <bean id="asta4dConfiguration" class="com.astamuse.asta4d.web.WebApplicationConfiguration">
        <property name="snippetResolver">
            <bean class="com.astamuse.asta4d.misc.spring.SpringManagedSnippetResolver"/>
        </property>
        <property name="instanceResolverList">
            <list>
                <bean class="com.astamuse.asta4d.misc.spring.SpringManagedInstanceResolver"/>
            </list>
        </property>
    </bean>

      
The SpringManagedSnippetResolver will retrieve snippet instance from Spring container and the SpringManagedInstanceResolver will also retrieve request handler instance from Spring container. Further the SpringManagedSnippetResolver will treat the snippet name in html template files as bean id.

The following example shows a full image of Spring's configuration for Asta4D:

Example 17.3. 


<?xml version="1.0" encoding="UTF-8"?>
    <bean id="asta4dConfiguration" class="com.astamuse.asta4d.web.WebApplicationConfiguration">
        <property name="snippetResolver">
            <bean class="com.astamuse.asta4d.misc.spring.SpringManagedSnippetResolver"/>
        </property>
        <property name="instanceResolverList">
            <list>
                <bean class="com.astamuse.asta4d.misc.spring.SpringManagedInstanceResolver"/>
            </list>
        </property>
        <property name="pageInterceptorList">
            <list>
                <bean class="com.astamuse.asta4d.sample.interceptor.SamplePageInterceptor"/>
            </list>
        </property>
        <property name="snippetInvoker">
            <bean id="snippetInvoker" class="com.astamuse.asta4d.snippet.DefaultSnippetInvoker">
                <property name="snippetInterceptorList">
                    <list>
                        <bean class="com.astamuse.asta4d.sample.interceptor.SampleSnippetInterceptor"/>
                    </list>
                </property>
            </bean>        
        </property>
        <property name="urlMappingRuleInitializer">
            <bean class="com.astamuse.asta4d.sample.UrlRules"/>
        </property>
    </bean>
    
    <bean id="ComplicatedSnippet" class="com.astamuse.asta4d.sample.snippet.ComplicatedSnippet" scope="prototype"/>
    <bean id="FormSnippet" class="com.astamuse.asta4d.sample.snippet.FormSnippet" scope="prototype"/>
    <bean id="SimpleSnippet" class="com.astamuse.asta4d.sample.snippet.SimpleSnippet" scope="prototype"/>
    <bean id="ShowCodeSnippet" class="com.astamuse.asta4d.sample.snippet.ShowCodeSnippet" scope="prototype"/>
    
    <bean class="com.astamuse.asta4d.sample.handler.LoginHandler"/>

      
Note that Asta4D always treats snippet instance as singleton per request for field injection on snippet, so the scope of snippet beans should be "prototype" basically except there is no field injection in snippet implementation. For the request handlers, "prototype" is not necessary since there is no field injection on request handler.

17.2. Integrate with Spring MVC

Asta4D's template engine can be integrated to Spring MVC as view solution too. The following configuration shows how:

Example 17.4. 


    <bean class="com.astamuse.asta4d.misc.spring.mvc.Asta4dTemplateInitializer"/>
    <bean class="com.astamuse.asta4d.misc.spring.mvc.SpringWebPageViewResolver"/>