This project has retired. For details please refer to its Attic page.
Qi4j in 2 minutes
Qi4j
Introduction
Tutorials
Javadoc
Samples
Core
Libraries
Extensions
Tools
Glossary 

Qi4j in 2 minutes

Tip

Theses tutorials are based on actual code found in the tutorials/ directory of the Qi4j SDK sources. You should start your favorite editor and find the code related to this tutorial, run it and play with it.

To show that Qi4j is not necessarily complex, not hard to get going with and easy to deploy, we are first showing the classic HelloWorld, as small as it can get and still be Composite Oriented Programming and not only standard OOP.

If you want to reproduce what’s explained in this tutorial, remember to depend on the Core Runtime artifact that depends on Core API, Core SPI, Core Bootstrap and Core Functional & I/O APIs:

Table 1. Artifact

Group IDArtifact IDVersion

org.qi4j.core

org.qi4j.core.runtime

2.0


See the Depend on Qi4j in your build tutorial for details.

Ready, Set, Go!

Let’s say we want to do the common HelloWorld example, but with a more domain-oriented setting. We have a Speaker interface that does the talking. But we also need an implementation for Speaker, which we declare here via the @Mixins( SpeakerMixin.class ).

@Mixins( SpeakerMixin.class )
public interface Speaker
{
    String sayHello();
}

And of course, the simple implementation of the Speaker interface. In this case, return a String with the content "Hello, World!".

public class SpeakerMixin
    implements Speaker
{
    @Override
    public String sayHello()
    {
        return "Hello, World!";
    }
}

So far so good. We now need to make this into something that can run. This can be done like this;

public class Main
{
    public static void main( String[] args )
        throws Exception
    {
        SingletonAssembler assembler = new SingletonAssembler() // <1>
        {
            @Override
            public void assemble( ModuleAssembly assembly )
                throws AssemblyException
            {
                assembly.transients( Speaker.class );           // <2>
            }
        };
        Speaker speaker = assembler.module().newTransient( Speaker.class ); // <3>
        System.out.println( speaker.sayHello() );
    }
}
  1. The SingletonAssembler is a convenience class that creates a Qi4j Runtime instance and an application with one layer and one module in it.
  2. We declare a TransientComposite of type Speaker.
  3. We create the Composite instance from the Module.

Done!