9

Spring には、 PropertyPlaceholderConfigurerと呼ばれる非常に便利な便利なクラスがあります。これは、標準の .properties ファイルを取り、そこから値を bean.xml 構成に注入します。

まったく同じことを行い、同じ方法でSpringと統合するが、構成用のXMLファイルを受け入れるクラスを知っている人はいますか? 具体的には、Apache ダイジェスター スタイルの構成ファイルを考えています。これを行うのは簡単です。誰かがすでに持っているかどうか疑問に思っています。

提案?

4

4 に答える 4

7

これをテストしたところ、うまくいくはずです。

PropertiesPlaceholderConfigurer には setPropertiesPersister メソッドが含まれているため、PropertiesPersisterの独自のサブクラスを使用できます。デフォルトの PropertiesPersister は、すでに XML 形式のプロパティをサポートしています。

完全に機能するコードを表示するだけです。

JUnit 4.4 テスト ケース:

package org.nkl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ContextConfiguration(locations = { "classpath:/org/nkl/test-config.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class PropertyTest {

    @Autowired
    private Bean bean;

    @Test
    public void testPropertyPlaceholderConfigurer() {
        assertNotNull(bean);
        assertEquals("fred", bean.getName());
    }
}

春の設定ファイルtest-config.xml

<?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:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
  <context:property-placeholder 
      location="classpath:/org/nkl/properties.xml" />
  <bean id="bean" class="org.nkl.Bean">
    <property name="name" value="${org.nkl.name}" />
  </bean>
</beans>

XML プロパティ ファイルproperties.xml-使用方法の説明については、こちらを参照してください。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
  <entry key="org.nkl.name">fred</entry>
</properties>

そして最後に豆:

package org.nkl;

public class Bean {
    private String name;
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

お役に立てれば...

于 2009-01-26T16:32:28.933 に答える
4

Spring Modules は、Spring と、階層的な XML 構成スタイルを持つCommons Configuration との統合を提供することがわかりました。これは、まさに私が望んでいた PropertyPlaceholderConfigurer に直結しています。

于 2009-01-29T13:32:12.470 に答える
2

Apache ダイジェスター スタイルの構成ファイルについてはよくわかりませんが、実装がそれほど難しくなく、xml 構成ファイルに適したソリューションを見つけました。

春から通常の PropertyPlaceholderConfigurer を使用できますが、カスタム構成を読み取るには、独自の PropertiesPersister を作成する必要があります。ここで、xml を (XPath で) 解析し、必要なプロパティを自分で設定できます。

以下に小さな例を示します。

まず、デフォルトの PropertiesPersister を拡張して独自の PropertiesPersister を作成します。

public class CustomXMLPropertiesPersister extends DefaultPropertiesPersister {
            private XPath dbPath;
            private XPath dbName;
            private XPath dbUsername;
            private XPath dbPassword;

            public CustomXMLPropertiesPersister() throws JDOMException  {
                super();

            dbPath = XPath.newInstance("//Configuration/Database/Path");
            dbName = XPath.newInstance("//Configuration/Database/Filename");
            dbUsername = XPath.newInstance("//Configuration/Database/User");
            dbPassword = XPath.newInstance("//Configuration/Database/Password");
        }

        public void loadFromXml(Properties props, InputStream is)
        {
            Element rootElem = inputStreamToElement(is);

            String path = "";
            String name = "";
            String user = "";
            String password = "";

            try
            {
                path = ((Element) dbPath.selectSingleNode(rootElem)).getValue();
                name = ((Element) dbName.selectSingleNode(rootElem)).getValue();
                user = ((Element) dbUsername.selectSingleNode(rootElem)).getValue();
                password = ((Element) dbPassword.selectSingleNode(rootElem)).getValue();
            }
            catch (JDOMException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            props.setProperty("db.path", path);
            props.setProperty("db.name", name);
            props.setProperty("db.user", user);
            props.setProperty("db.password", password);
        }

        public Element inputStreamToElement(InputStream is)
        {       
            ...
        }

        public void storeToXml(Properties props, OutputStream os, String header)
        {
            ...
        }
    }

次に、CustomPropertiesPersister をアプリケーション コンテキストの PropertyPlaceholderConfigurer に挿入します。

<beans ...>
    <bean id="customXMLPropertiesPersister" class="some.package.CustomXMLPropertiesPersister" />

    <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_FALLBACK" />
        <property name="location" value="file:/location/of/the/config/file" />
        <property name="propertiesPersister" ref="customXMLPropertiesPersister" />
    </bean> 
</beans>

その後、次のようにプロパティを使用できます。

<bean id="someid" class="some.example.class">
  <property name="someValue" value="$(db.name)" />
</bean>
于 2011-05-25T09:05:59.177 に答える