Skip to content
This repository was archived by the owner on May 10, 2019. It is now read-only.

Providing Services: Automatically Way

miere edited this page Oct 14, 2014 · 1 revision

The @Singleton annotation tells tRip Context that the annotated class should have only one instance of it per context. If you store data at the class members it will be there the next time you ask for him to tRip Context. In other words, there will be only one instance of a Singleton managed class for a specific ServiceProvider instances.

The @Stateless annotation tells exactly opposed information to tRip Context: a fresh instance of this service class will be create the next time you ask for him.

For example, you could expose a Runnable Singleton Service as done next:

@Singleton( exposedAs=Runnable.class )
public class MyJob implements Runnable {}

You also could expose it as a Runnable Stateless Service as done next:

@Stateless( exposedAs=Runnable.class )
public class MyJob implements Runnable {}

In both cases, you could use the same implementation to consume those services:

// sample usage:
ServiceProvider provider = new ServiceProvider()
Runnable job = provider.load( Runnable.class );
job.run();

You also could the class it self as a Singleton ( or Stateless ) Service:

@Singleton
public class MyJob{}

// sample usage:
ServiceProvider provider = new ServiceProvider()
MyJob job = provider.load( MyJob.class );