Qi4j

Javabean Wrapping

Content goes here...
The wrapping support is intended to be used where an existing subsystem expects and delivers Javabeans instead of Qi4j composites. This is an important part of the legacy support that Qi4j provides.

Let's look at an example. We have an existing interface to a system that returns javabeans;

public interface LibraryPojo
{
    BookBean findBook( String isbn );
}

public class BookBean
{
    private String title;
    private String isbn;
    private Person author;

    public String getTitle()
    {
        return title;
    }

    public String getIsbn()
    {
        return isbn;
    }

    public Person getAuthor()
    {
        return author;
    }

    public void setTitle( String title )
    {
        this.title = title;
    }

    public void setIsbn( String isbn )
    {
        this.isbn = isbn;
    }

    public void setAuthot( Person author )
    {
        this.author = author;
    }
}

And that we want to make the Libary available as a Qi4j service and have an automatic wrapping of the Book and Person classes.

We need to map the Qi4j model against the existing javabeans according to a strict set of rules. Among other things;

  • The Book and Person composite types must implement the JavabeanSupport mixin type.
  • For each getter there must be one Property with the same name in lower-case.
  • If no setter exist, a ImmutableProperty should be used.
  • Write-only javabeans properties are not supported.

And we will end up with something like this;

public interface Library
{
    Book findBook( String isbn );
}

@Mixins( LibraryMixin.class )
public interface LibraryService extends Library, ServiceComposite
{}

public interface Book
{
    Property<String> title();
    Property<String> isbn();
    Property<Person> author();
}

public interface BookComposite extends Book, JavabeanSupport, Composite
{}

public interface Person
{
    Property....
}

public interface PersonComposite extends Person, JavabeanSupport, Composite
{}

So, now we have the mapping in place. How do we put it into action?

Well, you need to write the Qi4j Library interface which calls the LibraryPojo in whatever way that happens to be, and then build the result Book object with the return BookBean as the javabean backing the composite. It looks something like this;

public class LibraryMixin
    implements Library
{
    @Structure private CompositeBuilderFactory factory;
    @Uses private LibraryPojo library;
    
    public Book findBook( String isbn )
    {
        BookBean bean = library.findBook( isbn );

        CompositeBuilder<Book> builder = factory.newCompositeBuilder( Book.class );
        builder.use( bean );
        return builder.newInstance();
    }
}

We leave out how the LibraryPojo instance get in there, and just note that there is very little we need to do, to get this to work. Just create a Book composite which uses the BookBean instance retrieved.
When the author (type Person) is retrieved, the JavabeanSupport will automatically do what we just did in the code above.

Qi4j and the Qi4j logo are trademarks of Richard Öberg, Niclas Hedhman and the members of the Qi4j Core Team. See Qi4j licensing for more information.
Powered by SiteVisionexternal link.