开发者

Dozer BeanFactory: How to implement it?

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'v开发者_如何学JAVAe Googled and all I see are just jars and sources of jars.


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).


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜