Skip to content
This repository has been archived by the owner on Feb 9, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
[inject] adding new sample demonstrating the staticless manual inject…
…ion approach
  • Loading branch information
ph2734 committed May 14, 2012
1 parent ed0bd09 commit 6e8d331
Show file tree
Hide file tree
Showing 21 changed files with 264 additions and 23 deletions.
63 changes: 63 additions & 0 deletions inject/README.md
Expand Up @@ -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 {
Expand Down Expand Up @@ -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```)

Expand Down
2 changes: 1 addition & 1 deletion inject/sample/app/controllers/Application.java
Expand Up @@ -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()));
Expand Down
6 changes: 1 addition & 5 deletions inject/sample/app/views/index.scala.html
@@ -1,7 +1,3 @@
@(message: String)

@main("Welcome to Play 2.0") {

@play20.welcome(message, style = "Java")

}
@message
15 changes: 0 additions & 15 deletions inject/sample/app/views/main.scala.html

This file was deleted.

2 changes: 1 addition & 1 deletion inject/sample/project/Build.scala
Expand Up @@ -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"

)

Expand Down
2 changes: 1 addition & 1 deletion inject/sample/project/plugins.sbt
Expand Up @@ -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")
14 changes: 14 additions & 0 deletions inject/sample_without_static_field/app/Global.java
@@ -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()));
}

}
38 changes: 38 additions & 0 deletions inject/sample_without_static_field/app/module/Dependencies.java
@@ -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();}
};
}


}
12 changes: 12 additions & 0 deletions inject/sample_without_static_field/app/service/MyService.java
@@ -0,0 +1,12 @@
package service;

public abstract class MyService {

protected Something s;

public MyService(Something s) {
this.s = s;
}

abstract public String demonstrate();
}
7 changes: 7 additions & 0 deletions inject/sample_without_static_field/app/service/Something.java
@@ -0,0 +1,7 @@
package service;

public interface Something {

public String noWhat();

}
4 changes: 4 additions & 0 deletions inject/sample_without_static_field/app/views/index.scala.html
@@ -0,0 +1,4 @@
@(message: String)

@message

57 changes: 57 additions & 0 deletions inject/sample_without_static_field/conf/application.conf
@@ -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

1 change: 1 addition & 0 deletions inject/sample_without_static_field/conf/play.plugins
@@ -0,0 +1 @@
1500:com.typesafe.plugin.inject.ManualInjectionPlugin
9 changes: 9 additions & 0 deletions inject/sample_without_static_field/conf/routes
@@ -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)
19 changes: 19 additions & 0 deletions inject/sample_without_static_field/project/Build.scala
@@ -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
8 changes: 8 additions & 0 deletions inject/sample_without_static_field/project/plugins.sbt
@@ -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.

Large diffs are not rendered by default.

Empty file.

0 comments on commit 6e8d331

Please sign in to comment.