Permalink
Browse files

[inject] adding new sample demonstrating the staticless manual inject…

…ion approach
  • Loading branch information...
1 parent ed0bd09 commit 6e8d331450cdf419dccb70e6f5929594ed3d2ffb @pk11 pk11 committed May 14, 2012
View
@@ -7,6 +7,8 @@ These would be injected into a preconfigured package (```controllers``` by defau
* Allows static field injection to a preconfigured package (```controllers``` by default) ie
+(see ```sample``` for a full example)
+
```java
// define your dependencies in module/Dependencies.java
public class Dependencies {
@@ -41,6 +43,67 @@ public class Application extends Controller {
```
+* or you can use constructor injection with a delege
+(see ```sample_without_static_field``` for a full example)
+
+in ```app/controllers/Application.java```:
+
+```java
+public class Application extends Controller {
+
+ private MyService s;
+
+ @Inject public Application( MyService s) {
+ this.s=s;
+ }
+
+ public Result index() {
+ return ok(index.render(s.demonstrate()));
+ }
+
+}
+```
+
+in ```app/module/Dependencies.java```:
+
+```
+public class Dependencies {
+
+ public static InjectPlugin inject() {
+ return Play.application().plugin(InjectPlugin.class);
+ }
+
+ public static controllers.Application application() {
+ return inject().getInstance(controllers.Application.class);
+ }
+
+ @Provides
+ @Singleton
+ public Something makeSomething() {
+ return new Something() {
+ public String noWhat() {
+ return "yay";
+ }
+ };
+ }
+
+ @Provides·
+ @Singleton
+ public MyService makeService(Something s) {
+ return new MyService(s) {
+ public String demonstrate() { return s.noWhat();}
+ };
+ }
+
+}
+```
+
+
+in ```conf/routes```:
+
+```
+GET / module.Dependencies.application.index
+```
* Dependency modules are configurable (by default it's ```module.Dependencies```)
@@ -10,7 +10,7 @@
public class Application extends Controller {
- @Inject static MyService s;
+ @Inject MyService s;
public static Result index() {
return ok(index.render(s.demonstrate()));
@@ -1,7 +1,3 @@
@(message: String)
-@main("Welcome to Play 2.0") {
-
- @play20.welcome(message, style = "Java")
-
-}
+@message
@@ -1,15 +0,0 @@
-@(title: String)(content: Html)
-
-<!DOCTYPE html>
-
-<html>
- <head>
- <title>@title</title>
- <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
- <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
- <script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
- </head>
- <body>
- @content
- </body>
-</html>
@@ -8,7 +8,7 @@ object ApplicationBuild extends Build {
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
- "com.typesafe" % "play-plugins-inject" % "2.0.1"
+ "com.typesafe" % "play-plugins-inject" % "2.0.2"
)
@@ -5,4 +5,4 @@ logLevel := Level.Warn
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
// Use the Play sbt plugin for Play projects
-addSbtPlugin("play" % "sbt-plugin" % "2.0-RC3")
+addSbtPlugin("play" % "sbt-plugin" % "2.0.1")
@@ -0,0 +1,14 @@
+import play.*;
+import play.libs.*;
+
+import java.util.*;
+
+import service.MyService;
+import com.typesafe.plugin.inject.InjectPlugin;
+
+public class Global extends GlobalSettings {
+
+ public void onStart(Application app) {
+ Logger.warn("getting an instance from guice:"+ app.plugin(InjectPlugin.class).getInstance(MyService.class));
+ }
+}
@@ -0,0 +1,23 @@
+package controllers;
+
+import play.*;
+import play.mvc.*;
+
+import views.html.*;
+
+import javax.inject.*;
+import service.*;
+
+public class Application extends Controller {
+
+ private MyService s;
+
+ @Inject public Application( MyService s) {
+ this.s=s;
+ }
+
+ public Result index() {
+ return ok(index.render(s.demonstrate()));
+ }
+
+}
@@ -0,0 +1,38 @@
+package module;
+
+import com.google.inject.Provides;
+import javax.inject.Singleton;
+import service.*;
+import play.Play;
+import com.typesafe.plugin.inject.InjectPlugin;
+
+public class Dependencies {
+
+ public static InjectPlugin inject() {
+ return Play.application().plugin(InjectPlugin.class);
+ }
+
+ public static controllers.Application application() {
+ return inject().getInstance(controllers.Application.class);
+ }
+
+ @Provides
+ @Singleton
+ public Something makeSomething() {
+ return new Something() {
+ public String noWhat() {
+ return "yay";
+ }
+ };
+ }
+
+ @Provides
+ @Singleton
+ public MyService makeService(Something s) {
+ return new MyService(s) {
+ public String demonstrate() { return s.noWhat();}
+ };
+ }
+
+
+}
@@ -0,0 +1,12 @@
+package service;
+
+public abstract class MyService {
+
+ protected Something s;
+
+ public MyService(Something s) {
+ this.s = s;
+ }
+
+ abstract public String demonstrate();
+}
@@ -0,0 +1,7 @@
+package service;
+
+public interface Something {
+
+ public String noWhat();
+
+}
@@ -0,0 +1,4 @@
+@(message: String)
+
+@message
+
@@ -0,0 +1,57 @@
+# This is the main configuration file for the application.
+# ~~~~~
+
+# Secret key
+# ~~~~~
+# The secret key is used to secure cryptographics functions.
+# If you deploy your application to several instances be sure to use the same key!
+application.secret="cBlQ[B0FM]DiFD<KIq6I`1TM81lna:gCo2XFAwdXo2lUIH;c:eL=_=N]o@UoblHu"
+
+# The application languages
+# ~~~~~
+application.langs="en"
+
+# Global object class
+# ~~~~~
+# Define the Global object class for this application.
+# Default to Global in the root package.
+# global=Global
+
+# Database configuration
+# ~~~~~
+# You can declare as many datasources as you want.
+# By convention, the default datasource is named `default`
+#
+# db.default.driver=org.h2.Driver
+# db.default.url="jdbc:h2:mem:play"
+# db.default.user=sa
+# db.default.password=
+#
+# You can expose this datasource via JNDI if needed (Useful for JPA)
+# db.default.jndiName=DefaultDS
+
+# Evolutions
+# ~~~~~
+# You can disable evolutions if needed
+# evolutionplugin=disabled
+
+# Ebean configuration
+# ~~~~~
+# You can declare as many Ebean servers as you want.
+# By convention, the default server is named `default`
+#
+# ebean.default="models.*"
+
+# Logger
+# ~~~~~
+# You can also configure logback (http://logback.qos.ch/), by providing a logger.xml file in the conf directory .
+
+# Root logger:
+logger.root=ERROR
+
+# Logger used by the framework:
+logger.play=INFO
+
+# Logger provided to your application:
+logger.application=DEBUG
+
@@ -0,0 +1 @@
+1500:com.typesafe.plugin.inject.ManualInjectionPlugin
@@ -0,0 +1,9 @@
+# Routes
+# This file defines all application routes (Higher priority routes first)
+# ~~~~
+
+# Home page
+GET / module.Dependencies.application.index
+
+# Map static resources from the /public folder to the /assets URL path
+GET /assets/*file controllers.Assets.at(path="/public", file)
@@ -0,0 +1,19 @@
+import sbt._
+import Keys._
+import PlayProject._
+
+object ApplicationBuild extends Build {
+
+ val appName = "j"
+ val appVersion = "1.0-SNAPSHOT"
+
+ val appDependencies = Seq(
+ "com.typesafe" % "play-plugins-inject" % "2.0.2"
+
+ )
+
+ val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
+ // Add your own project settings here
+ )
+
+}
@@ -0,0 +1 @@
+sbt.version=0.11.3
@@ -0,0 +1,8 @@
+// Comment to get more information during initialization
+logLevel := Level.Warn
+
+// The Typesafe repository
+resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
+
+// Use the Play sbt plugin for Play projects
+addSbtPlugin("play" % "sbt-plugin" % "2.0.1")
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.

0 comments on commit 6e8d331

Please sign in to comment.