Announcement Announcement Module
Collapse
No announcement yet.
Can one define a placeholder within configuration file Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can one define a placeholder within configuration file

    Is it possible to define a placeholder within the Spring configuration file, say simliar to the Ant "property".

    I understand how PropertyPlaceholderConfigurer works, but it is not always a beans property value that I want to shortcut.

    I would like to shortcut a classname that I use repeatedly within the config.

    Just some psuedo of what I am looking for:

    <define property="thisclass" value="com.xyz.MyClass" />

    <bean id="myId" class="${thisclass}" >
    ...
    </bean>

    Thanks for any info/solutions!

  • #2
    Hi,

    I just had the same problem. I did not find a direct solution, but a I think I found a good work arround:

    I implemented a BeanFactory, creating a class per Reflection. The class name can be set using a property. Of course, my simple implementation does only work with bean having no constructor parameter.

    Code:
    public class ReflectionFactory extends AbstractFactoryBean implements
        FactoryBean &#123;
    
      private String className;
    
      public void setClassName&#40;String className&#41; &#123;
        this.className = className;
      &#125;
    
      protected Object createInstance&#40;&#41; throws Exception &#123;
        return getObjectType&#40;&#41;.newInstance&#40;&#41;;
      &#125;
    
      public Class getObjectType&#40;&#41; &#123;
        try &#123;
          return Class.forName&#40;className&#41;;
        &#125; catch &#40;ClassNotFoundException e&#41; &#123;
          throw new IllegalArgumentException&#40;"Given Classname " + className
              + " not found.", e&#41;;
        &#125;
      &#125;
    
    &#125;
    greetings
    Robert

    Comment


    • #3
      I forgot:

      Then you can use the factory bean in following way:

      Code:
      <bean id="namingStrategy" class="ReflectionFactory">
      		<property name="className" value="$&#123;tas.namingStrategy&#125;" />
         </bean>
      Robert

      Comment


      • #4
        You can always use the parent attribute.

        Code:
        <bean id="baseId" class="yourpkg.yourclass" abstract="true"> 
        ...can specify any properties that should be reused... 
        </bean> 
        
        <bean id="myId" parent="baseId" > 
        ... can override properties from the base..
        </bean>

        Comment

        Working...
        X