Results 1 to 9 of 9

Thread: Replace tokens in an xml file with import tag

  1. #1
    Join Date
    Oct 2007
    Posts
    7

    Default Replace tokens in an xml file with import tag

    Hello,

    Is it possible that we can put tokens in an xml file and replace those tokens with the import tag in Spring?

    e.g.
    1. In commonBeans.xml

    <bean id="handlerMapping" class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
    <property name="interceptors">
    <list>
    <ref bean="myInterceptor"/>
    </list>
    <property name="mappings">
    <value>
    @mapping.token@=@controller.token@
    </value>
    </property>
    </bean>

    <bean id="myInterceptor" class="com.MyInterceptor"/>

    2. In servlet context xml

    <import resource="commonBeans.xml">
    <replace name="@mapping.token@" value="/myMapping*"/>
    <replace name="@controller.token@" value="/myController*"/>
    </import>

    thanks a lot

  2. #2
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Your example looks like an Ant example. Are you aware of Spring's configuration customization capabilities?

    Joerg
    This post can contain insufficient information.

  3. #3
    Join Date
    Oct 2007
    Posts
    7

    Default

    Yes, it looks like an ant example. But the idea is simple, just avoid so many copy and paste common bean definitions to every application context file.

    We can abstract those beans into a common bean definition file, and import it into the application context using the import tag. The problem is that some beans need customized during the import.

    With token replacement in import tag, our context files are much clean and maintainable

    Could you please give me something hints or examples use the BeanFactoryPostProcessor for this?

  4. #4
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    I had a particular one in mind, the PropertyPlaceholderConfigurer.

    Joerg
    This post can contain insufficient information.

  5. #5
    Join Date
    Oct 2007
    Posts
    7

    Default Replace tokens in an xml file with import tag

    The problem here is that a token may not be a property, just a placeholder. I got the exception say something like the 'mapping.token' is not a property with the PropertyPlaceholderConfigurer.

    Any suggestion?

    Thank you very much.

  6. #6
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    How exactly did you write it?

    Joerg
    This post can contain insufficient information.

  7. #7
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    It would be useful to see what you were trying to do, but as a guess..... did you set the placeholderPrefix and placeholderSuffix.
    http://static.springframework.org/sp...onfigurer.html
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  8. #8
    Join Date
    Oct 2007
    Posts
    7

    Default Replace tokens in an xml file with import tag

    Thanks a lot for your replies.

    I have 4 DispatcherServlet denfied in web.xml,
    <servlet>
    <servlet-name>servlet1</servlet-name>
    <display-name>Servlet 1</display-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    ...
    <servlet>
    <servlet-name>servlet4</servlet-name>
    <display-name>Servlet 4</display-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

    The application context file of each servlet (1 ... 4) has some common bean denification. Please see the commonBeans.xml in the beginning of this thread.
    Only the url pattern and controller name are different in each servlet. The url-pattern and controller name are not any property value, just placeholders.

    Right now, I copied the content of the commonBeans.xml into those 4 servlets.

    What I wnat to do this that using the import-tag imports the commonBeans.xml and replace those tokens during the improting. Please see an example in the beginning of the thread.

    What I tried out were:

    1. In application context file
    <import resource="commonBeans.xml">
    <property name="@urlmapping.token@" value="myUrl*"/>
    <property name="@controller.token@" value="myContorller*"/>
    </import>
    Got the 'The content of element type "import" must match "EMPTY" ' exception

    2. Using the PropertyPlaceholderConfigurer with a properties file, there are two problems:
    a. each servlet need a property file (It's too overhead, and gets messing)
    b. the token is not a property, once I used
    <property name="@mapping.token@" value="${url.mapping}"/>
    and got the 'mapping.token' is not a property exception.

    I realized that the token replace maybe not possible with import tag for the moment, but it would be nice spring support it later. With token replace, we needn't copy and paste the common bean definition into every servlet application context.

    Thanks a lot.

  9. #9
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    1. I'm pretty sure placeholder replacing does not work for property names.
    2. The @token@ syntax is of course not known to Spring.


    Joerg
    This post can contain insufficient information.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •