|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
Does your PropertyPlaceholderConfigurer resolve any other system properties or just this one? Is it possible to see the actual bean definition for your PropertyPlaceholderConfigurer?
|
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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?
|
|
#5
|
|||
|
|||
|
Quote:
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);
}
}
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());
}
}
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>
|
|
#6
|
|||
|
|||
|
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}" />
http://issues.compass-project.org/browse/CMP-457 |
![]() |
| Thread Tools | |
| Display Modes | |
|
|