从 XML Spring.NET IoC 配置设置 XmlElement 类型的构造函数参数

发布于 2024-11-08 23:27:08 字数 718 浏览 4 评论 0原文

想象一下:

class Foo {
  public Foo(XmlElement xml) { ... }
}

我想使用 Spring.NET 和 XmlApplicationContext 实例化此类。 生成 XmlElement 的 XML 应包含在 XmlApplicationContext 配置文件中,以便可以轻松编辑。

所以它应该看起来像这样:

<objects>
  <object id="foo" type="Foo, Foo">
    <constructor-arg name="xml" ???>
      <???>
        <element1 attr="bla" />
        <element2 xyz="abc>
          <... />
        </element2>
      </???>
    </constructor-arg>
  </object>
</objects>

元素 应该是注入的 XmlElement。

有什么办法可以实现这一点吗?

我知道我可以传递文件名并手动加载内部 XML。如果没有其他办法,这将是解决方案。但为了方便用户,我最喜欢“嵌入式 XML”解决方案:-)

Imagine the following:

class Foo {
  public Foo(XmlElement xml) { ... }
}

I want to instantiate this class using Spring.NET and XmlApplicationContext.
The XML the XmlElement is generated from should be contained in the XmlApplicationContext configuration file so it can be edited easily.

So it should look something like this:

<objects>
  <object id="foo" type="Foo, Foo">
    <constructor-arg name="xml" ???>
      <???>
        <element1 attr="bla" />
        <element2 xyz="abc>
          <... />
        </element2>
      </???>
    </constructor-arg>
  </object>
</objects>

The element <???> should be the XmlElement injected.

Is there any way to achieve this?

I know I could pass a filename and load the inner XML by hand. This will be the solution if there is no way to do other. But for convenience to the user I most like the "embedded XML" solution :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荭秂 2024-11-15 23:27:08

您可以使用 static工厂

public static class XmlElementFactory
{
    public static XmlElement Create(string value)
    {
        var doc = new XmlDocument();
        doc.LoadXml(value);
        return doc.DocumentElement;
    }
}

public class Foo
{
    private readonly XmlElement _xml;
    public Foo(XmlElement xml)
    {
        _xml = xml;
    }

    public override string ToString()
    {
        return _xml.OuterXml;
    }
}

class Program
{
    static void Main()
    {
        var foo = (Foo)ContextRegistry.GetContext().GetObject("foo");
        Console.WriteLine(foo);
    }
}

并在配置文件中:

<configuration>
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>

  <spring>
    <context>
      <resource uri="config://spring/objects"/>
    </context>

    <objects xmlns="http://www.springframework.net">
      <object id="foo" type="MyNs.Foo">
        <constructor-arg name="xml">
          <object type="MyNs.XmlElementFactory" factory-method="Create">
            <constructor-arg name="value">
              <value>
                <![CDATA[
                <root>
                  <element1 attr="bla" />
                  <element2 xyz="abc">
                  </element2>
                </root>
                ]]>
              </value>
            </constructor-arg>
          </object>
        </constructor-arg>
      </object>
    </objects>
  </spring>  
</configuration>

You could use a static factory and <![CDATA[ ... ]]>:

public static class XmlElementFactory
{
    public static XmlElement Create(string value)
    {
        var doc = new XmlDocument();
        doc.LoadXml(value);
        return doc.DocumentElement;
    }
}

public class Foo
{
    private readonly XmlElement _xml;
    public Foo(XmlElement xml)
    {
        _xml = xml;
    }

    public override string ToString()
    {
        return _xml.OuterXml;
    }
}

class Program
{
    static void Main()
    {
        var foo = (Foo)ContextRegistry.GetContext().GetObject("foo");
        Console.WriteLine(foo);
    }
}

and in the config file:

<configuration>
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>

  <spring>
    <context>
      <resource uri="config://spring/objects"/>
    </context>

    <objects xmlns="http://www.springframework.net">
      <object id="foo" type="MyNs.Foo">
        <constructor-arg name="xml">
          <object type="MyNs.XmlElementFactory" factory-method="Create">
            <constructor-arg name="value">
              <value>
                <![CDATA[
                <root>
                  <element1 attr="bla" />
                  <element2 xyz="abc">
                  </element2>
                </root>
                ]]>
              </value>
            </constructor-arg>
          </object>
        </constructor-arg>
      </object>
    </objects>
  </spring>  
</configuration>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文