Tuesday, July 8, 2014

Inject OSGi services in Sling models (Pojos) - Sling model way

Create the injection point in the sling model class with @Source annotation & assign "osgi-services" as a value to the source annotation. 
@Model(adaptables = Resource.class)
public class Banners {
@Inject @Source("osgi-services") GreetService greetService;
view raw Banners.java hosted with ❤ by GitHub


By this we can inject the below OSGi service in the Sling Model class.

@Service(value = GreetService.class)
@Component( metatype = true, immediate = true )
public class GreetServiceImpl implements GreetService {
@Override
public String greet(String greeter) {
return String.format( "Hallo %s, Wel Come to Sling Model", greeter );
}
}

5 comments: