Q:

PlayFramework how to inject object to a template

what I'm trying to accomplish is to inject some spring repository object from context to any template.
There is already possibility to call any method from anywhere, including templates, but "any method" must be defined as static, in such case, from template we can call @Class.staticMethod(). I want to do same thing but I need retrieve the "Class" from spring context

A:

Basically all you need is a service locator with a static method which will allow to fetch bean from a Spring context. There're many ways to access a Spring context from a static method. Below I present one of them.
@Service
public class ServiceLocator {

    @Autowired
    private static ApplicationContext appContext;

    @Autowired
    public ServiceLocator(ApplicationContext appContext) {
        ServiceLocator.appContext = appContext;
    }

    public static <T> T getClassFromSpringContainer(Class<T> clazz) {
        return appContext.getBean(clazz);
    }

}

In a view you can import that class and fetch any bean you like.
@import models.ServiceLocator
@import models.SampleService

@ServiceLocator.getClassFromSpringContainer(classOf[SampleService]).sampleMethod()

