Community   SpringSource   Projects    Downloads    Documentation    Forums    Training   Exchange   Blogs

Go Back   Spring Community Forums > Core Spring Projects > Core Container

Reply
 
Thread Tools Display Modes
  #1  
Old Jan 18th, 2007, 02:29 PM
jeffkole jeffkole is offline
Junior Member
 
Join Date: Jan 2007
Posts: 2
Default PropertyPlaceholderConfigurer and XML schema bean definitions

In attempting to define a bean using the XML schema syntax, I find that the PropertyPlaceholderConfigurer is not recognizing that it needs to replace a property and thus the bean ends up with an invalid configuration.

My bean definition (happens to be a Compass object):

<compass:compass name="compass" txManager="transactionManager" dataSource="dataSource">
<compass:connection>
<compass:file path="${java.io.tmpdir}" />
</compass:connection>
</compass:compass>

The PropertyPlaceholderConfigurer runs and shows that it is able to resolve all of my other property references, but it does not even attempt to resolve the ${java.io.tmpdir} placeholder. The compass bean ends up using the string "${java.io.tmpdir}" as the name of the directory to write to, and that is not good at all.

Could this is be a bug in the Compass implementation of the beans or maybe just a misconfiguration?

Thanks.
Reply With Quote
  #2  
Old Jan 18th, 2007, 03:11 PM
karldmoore karldmoore is offline
Senior Member
 
Join Date: Sep 2006
Posts: 8,425
Default

Does your PropertyPlaceholderConfigurer resolve any other system properties or just this one? Is it possible to see the actual bean definition for your PropertyPlaceholderConfigurer?
Reply With Quote
  #3  
Old Jan 18th, 2007, 03:48 PM
jeffkole jeffkole is offline
Junior Member
 
Join Date: Jan 2007
Posts: 2
Default

The property placeholder actually resolves the same system property correctly for another bean. Here is the configuration:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
<property name="properties">
<ref local="applicationConfigurationProperties"/>
</property>
</bean>

"applicationConfigurationProperties" is a bean that uses the Commons configuration framework to expose a Properties object.
Reply With Quote
  #4  
Old Sep 6th, 2008, 08:31 AM
jwcarman jwcarman is offline
Junior Member
 
Join Date: Dec 2006
Posts: 15
Default Re: PropertyPlaceHolderConfigurer and XML schema bean definitions...

I'm having the same exact problem. Did you ever figure out what the problem was with this? Did you end up just manually creating the beans in your context configuration file rather than using the compass namespaced stuff to do so?
Reply With Quote
  #5  
Old Sep 7th, 2008, 01:25 AM
denis.zhdanov denis.zhdanov is offline
Senior Member
 
Join Date: May 2007
Location: Saint Petersburg, Russian Federation
Posts: 1,189
Default

Quote:
Originally Posted by jwcarman View Post
I'm having the same exact problem. Did you ever figure out what the problem was with this? Did you end up just manually creating the beans in your context configuration file rather than using the compass namespaced stuff to do so?
It looks pretty strange. The following code works fine for me. Can you test it at your environment?

ApplicationConfigurationFactoryBean.java
Code:
package com.spring;

import org.springframework.beans.factory.FactoryBean;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.CompositeConfiguration;

import java.util.Properties;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

public class ApplicationConfigurationFactoryBean implements FactoryBean {

    private List<Configuration> configurations = new ArrayList<Configuration>();

    public Object getObject() throws Exception {
        CompositeConfiguration compositeConfiguration = new CompositeConfiguration();
        for (Configuration configuration : configurations) {
            compositeConfiguration.addConfiguration(configuration);
        }

        Properties result = new Properties();
        Iterator iterator = compositeConfiguration.getKeys();
        while (iterator.hasNext()) {
            String key = iterator.next().toString();
            result.setProperty(key, compositeConfiguration.getProperty(key).toString());
        }
        return result;
    }

    public Class getObjectType() {
        return Properties.class;
    }

    public boolean isSingleton() {
        return true;
    }

    public void setConfigurations(List<Configuration> configurations) {
        this.configurations.addAll(configurations);
    }
}
SpringStart.java
Code:
package com.spring;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;

public class SpringStart {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
        TestClass bean = (TestClass) context.getBean("test");
        System.out.println(bean.getField());
    }
}
config.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="properties">
            <bean class="com.spring.ApplicationConfigurationFactoryBean">
                <property name="configurations">
                    <util:list>
                        <bean class="org.apache.commons.configuration.SystemConfiguration"/>
                    </util:list>
                </property>
            </bean>
        </property>
    </bean>

    <bean id="test" class="com.spring.TestClass">
        <property name="field" value="${java.io.tmpdir}"/>
    </bean>

</beans>
Reply With Quote
  #6  
Old Sep 7th, 2008, 06:29 AM
jwcarman jwcarman is offline
Junior Member
 
Join Date: Dec 2006
Posts: 15
Default PropertPlaceholderConfigurer and XML schema bean definitions

I actually figured out what was wrong. The way the namespace handler is written, it manually instantiates some objects rather than adding their definitions to the BeanDefinitionBuilder. When you do this, Spring doesn't get a chance to interpret those parameters. I wrote a simple namespace handler to test my theory. When I added the bean definition to the BeanDefinitionBuilder, it worked fine.

Code:
public class CustomNamespaceHandler extends NamespaceHandlerSupport
{
    private static final String FOO_ELEMENT = "foo";

    public CustomNamespaceHandler()
    {
    }

    public void init()
    {
        registerBeanDefinitionParser(FOO_ELEMENT, new FooParser());
    }

    private static class FooParser extends AbstractSingleBeanDefinitionParser
    {

        protected void doParse( Element element, BeanDefinitionBuilder beanDefinitionBuilder )
        {
            if( FOO_ELEMENT.equals( element.getLocalName()))
            {
                beanDefinitionBuilder.addPropertyValue("bar", element.getAttribute("bar"));
            }
        }

        private boolean isFooElement(Element element)
        {
            return FOO_ELEMENT.equals(element.getLocalName());
        }

        protected Class getBeanClass( Element element )
        {
            if( isFooElement(element) )
            {
                return Foo.class;
            }
            throw new RuntimeException("Unknown element " + element.getLocalName() );
        }
    }
}
Code:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="properties">
        <props>
            <prop key="propertyRef">Some Value!</prop>
        </props>
    </property>
</bean>
<custom:foo id="foo" bar="${propertyRef}" />
So, the problem is with how the Compass namespace handler is written. There's an issue filed for the problem with the Compass folks:

http://issues.compass-project.org/browse/CMP-457
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 09:36 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.