Spring将注释配置转换为XML

时间:2014-08-12 07:04:06

标签: java xml spring

我有以下代码片段,用于通过注释配置公开BridgePropertyPlaceholderConfigurer Bean:

    @Configuration
    public class SpringConfiguration {

        @Bean
        public static BridgePropertyPlaceholderConfigurer getProperties() {

            File conf = new File(new File(".").getAbsolutePath() + "/configuration.properties");

            BridgePropertyPlaceholderConfigurer configurer;
            configurer = new BridgePropertyPlaceholderConfigurer();

            Resource[] resources = {
                    new FileSystemResource(conf.getAbsolutePath())
            };

            configurer.setLocations(resources);

            return configurer;
        }
    }

基本上,我想公开一个与jar相同的文件夹中的属性文件。这就是为什么我用File

做疯狂的事情

无论如何,我可以将其转换为XML配置吗? 类似的东西:

    <bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
        <property name="location" value="#{new File('.')....}"/>
    </bean>

1 个答案:

答案 0 :(得分:0)

您可以使用工厂创建bean:

<bean id="bridgePropertyPlaceholderConfigurerFactory"
    class="foo.bar.BridgePropertyPlaceholderConfigurerFactory" />

<bean id="bridgePropertyPlaceholder"
    factory-bean="bridgePropertyPlaceholderFactory"
    factory-method="createBridgePropertyPlaceholderConfigurer" />

Java工厂:

public class BridgePropertyPlaceholderConfigurerFactory {
  public BridgePropertyPlaceholderConfigurer createBridgePropertyPlaceholderConfigurer() {
    // your creation code ..
    return ..
  }
}

有关工厂方法的更多详细信息,请查看documentation