Dozer BeanFactory:如何实现?

发布于 2024-10-07 04:23:12 字数 185 浏览 4 评论 0原文

我查看了 Dozer 的常见问题解答和文档,包括 SourceForge 论坛,但没有看到任何好的教程,甚至没有看到关于如何实现自定义 BeanFactory 的简单示例。

每个人都说,“只需实现一个 BeanFactory”。具体如何实施?

我用谷歌搜索过,我看到的只是罐子和罐子的来源。

I have looked at the Dozer's FAQs and docs, including the SourceForge forum, but I didn't see any good tutorial or even a simple example on how to implement a custom BeanFactory.

Everyone says, "Just implement a BeanFactory". How exactly do you implement it?

I've Googled and all I see are just jars and sources of jars.

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

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

发布评论

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

评论(3

茶底世界 2024-10-14 04:23:12

这是我的 BeanFactory 之一,我希望它有助于解释常见模式:

public class LineBeanFactory implements BeanFactory {

    @Override
    public Object createBean(final Object source, final Class<?> sourceClass, final String targetBeanId) {

        final LineDto dto = (LineDto) source;

        return new Line(dto.getCode(), dto.getElectrified(), dto.getName());

    }

}

以及相应的 XML 映射:

<mapping>

        <class-a bean-factory="com.floyd.nav.web.ws.mapping.dozer.LineBeanFactory">com.floyd.nav.core.model.Line</class-a>
        <class-b>com.floyd.nav.web.contract.dto.LineDto</class-b>

</mapping>

这样我声明,当需要 Line 的 new 实例时,它应该使用我的BeanFactory。这是一个单元测试,可以解释它:

@Test
public void Line_is_created_with_three_arg_constructor_from_LineDto() {

    final LineDto dto = createTransientLineDto();

    final Line line = (Line) this.lineBeanFactory.createBean(dto, LineDto.class, null);

    assertEquals(dto.getCode(), line.getCode());
    assertEquals(dto.getElectrified(), line.isElectrified());
    assertEquals(dto.getName(), line.getName());

}

所以 Object source 是映射的源 bean,Class sourceClass 是源 bean 的类(我忽略了它,因为它始终是一个 LineDto 实例)。 String targetBeanId 是目标 bean 的 ID(太被忽略了)。

Here is one of my BeanFactories, I hope it helps to explain the common pattern:

public class LineBeanFactory implements BeanFactory {

    @Override
    public Object createBean(final Object source, final Class<?> sourceClass, final String targetBeanId) {

        final LineDto dto = (LineDto) source;

        return new Line(dto.getCode(), dto.getElectrified(), dto.getName());

    }

}

And the corresponding XML mapping:

<mapping>

        <class-a bean-factory="com.floyd.nav.web.ws.mapping.dozer.LineBeanFactory">com.floyd.nav.core.model.Line</class-a>
        <class-b>com.floyd.nav.web.contract.dto.LineDto</class-b>

</mapping>

This way I declare that when a new instance of Line is needed then it should create it with my BeanFactory. Here is a unit test, that can explain it:

@Test
public void Line_is_created_with_three_arg_constructor_from_LineDto() {

    final LineDto dto = createTransientLineDto();

    final Line line = (Line) this.lineBeanFactory.createBean(dto, LineDto.class, null);

    assertEquals(dto.getCode(), line.getCode());
    assertEquals(dto.getElectrified(), line.isElectrified());
    assertEquals(dto.getName(), line.getName());

}

So Object source is the source bean that is mapped, Class sourceClass is the class of the source bean (I'm ignoring it, 'cause it will always be a LineDto instance). String targetBeanId is the ID of the destination bean (too ignored).

灯角 2024-10-14 04:23:12

自定义 Bean 工厂是一个具有创建 Bean 方法的类。有两种“风格”

a) 静态创建方法

SomeBean x = SomeBeanFactory.createSomeBean();

b) 实例创建方法

SomeBeanFactory sbf = new SomeBeanFactory();
SomeBean x = sbf.createSomeBean();

如果创建和设置 bean 需要一些棘手的逻辑(例如某些属性的初始值取决于外部配置文件),您将创建一个 bean 工厂。 bean 工厂类允许您集中有关如何创建如此棘手的 bean 的“知识”。其他类只是调用 create 方法,而不用担心如何正确创建这样的 bean。

A custom bean factory is a class that has a method that creates a bean. There are two "flavours"

a) static create method

SomeBean x = SomeBeanFactory.createSomeBean();

b) instance create method

SomeBeanFactory sbf = new SomeBeanFactory();
SomeBean x = sbf.createSomeBean();

You would create a bean factory if creating and setting up your bean requires some tricky logic, like for example initial value of certain properties depend on external configuration file. A bean factory class allows you to centralize "knowledge" about how to create such a tricky bean. Other classes just call create method without worying how to correctly create such bean.

酸甜透明夹心 2024-10-14 04:23:12

这是一个实际的实现。显然这没有多大意义,因为 Dozer 在没有 BeanFactory 的情况下也会做同样的事情,但你可以以不同的方式初始化它,而不是仅仅返回一个对象。

public class ComponentBeanFactory implements BeanFactory {

    @Override
    public Object createBean(Object source, Class<?> sourceClass,
            String targetBeanId) {
        return new ComponentDto();
    }

}

为什么你需要 BeanFactory 呢?也许这有助于理解你的问题。

Here is an actual implementation. Obviously it does not make a lot of sense, since Dozer would do the same without the BeanFactory, but instead of just returning an object, you could initialized it somehow differently.

public class ComponentBeanFactory implements BeanFactory {

    @Override
    public Object createBean(Object source, Class<?> sourceClass,
            String targetBeanId) {
        return new ComponentDto();
    }

}

Why do you need a BeanFactory anyways? Maybe that would help understanding your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文