请按照以下步骤操作:
第一步:在build.sbt文件中添加spring依赖。
libraryDependencies += "org.springframework" % "spring-context" % "4.1.6.RELEASE"
libraryDependencies += "org.springframework" % "spring-core" % "4.1.6.RELEASE"
libraryDependencies += "org.springframework" % "spring-beans" % "4.1.6.RELEASE"
libraryDependencies += "org.springframework" % "spring-aop" % "4.1.6.RELEASE"
第 2 步: 创建一个新类 (ApplicationGlobalSettings.java) 并使用 GlobalSettings 类实现。
package com.ranga.global.settings;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import play.Application;
import play.GlobalSettings;
public class ApplicationGlobalSettings extends GlobalSettings {
private static final String APPLICATION_CONTEXT_XML = "applicationContext.xml";
private ConfigurableApplicationContext applicationContext;
@Override
public void beforeStart(Application application) {
super.beforeStart(application);
}
@Override
public void onStart(Application application) {
super.onStart(application);
applicationContext = new ClassPathXmlApplicationContext(APPLICATION_CONTEXT_XML);
}
@Override
public void onStop(Application application) {
super.onStop(application);
if(applicationContext != null) {
applicationContext.close();
}
}
}
Step3:在conf文件夹下新建spring配置文件(applicationContext.xml)
conf\applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.ranga.services, com.ranga.daos"/>
</beans>
Step4:将新创建的 GlobalSettings 文件位置添加到应用程序配置文件(conf/application.conf)中。
.....some more configuration here.....
# Global Objects class
application.global=com.ranga.global.settings.ApplicationGlobalSettings
Step5:在com.ranga.service包(HelloWorldService.java)下新建一个服务类。
package com.ranga.services;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import com.ranga.daos.HelloWorldDAO;
@Service
public class HelloWorldService {
@Inject
private HelloWorldDAO helloWorldDAO;
public String sayHello() {
return helloWorldDAO.sayHello();
}
}
第六步:在com.ranga.daos包(HelloWorldDAO.java)下新建一个dao类。
package com.ranga.daos;
import org.springframework.stereotype.Repository;
@Repository
public class HelloWorldDAO {
public String sayHello() {
return "Hello Ranga!";
}
}
Step7:最后在Application.java文件中注入HelloWorldService。
package com.ranga.controllers;
import javax.inject.Inject;
import org.springframework.beans.factory.annotation.Autowired;
import com.ranga.services.HelloWorldService;
import play.*;
import play.mvc.*;
import views.html.*;
public class Application extends Controller {
@Inject
private HelloWorldService helloWorldService;
public Result index() {
return ok(index.render(helloWorldService.sayHello()));
}
}
Step8:最后修改index.scala.html文件代码。
@(message: String)
<h1>@message</h1>
现在完成.. 运行应用程序。