Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update from codebourne master #3

Merged
merged 1 commit into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
## Changelog

### 1.10

Move creation of guice injector from `onConfigurationRead()` to `onApplicationStart()`.
We found that method `onConfigurationRead()` can be called many times during application lifetime (on every application.conf change).
It causes creating multiple instances of guice injector, thus causing memory leaks.

### 1.9

* Added guava 23.0 dependency (Guice actually doesn't work without it)
* upgrade to play 1.5.1 (which adds methods Injector.setBeanSource() and automatically injects controllers' instances)

### 1.8

* upgrade to play 1.5

### 1.7

* create request.controllerInstance even if action method is static

### 1.6

* upgrade guice to 4.1.0 and include multibindings

### 1.5

* support injecting of non-static dependencies into controllers
Expand Down
7 changes: 4 additions & 3 deletions conf/dependencies.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
self: play-guice -> guice 1.8
self: play-guice -> guice 1.10

require:
- play 1.5.+
- com.google.inject -> guice 4.1.0:
transitive: true
transitive: false
- com.google.inject.extensions -> guice-multibindings 4.1.0:
transitive: false
- aopalliance 1.0:
transitive: true
transitive: false
- com.google.guava -> guava 23.0
17 changes: 10 additions & 7 deletions src/play/modules/guice/GuicePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,12 @@ public class GuicePlugin extends PlayPlugin implements BeanSource {

@Override
public void onApplicationStart() {
logger.debug("Starting Guice modules scanning");
logger.debug("Starting Guice modules injecting");
loadInjector();
play.inject.Injector.setBeanSource(this);
play.inject.Injector.inject(this);
injectAnnotated();
}

@Override public void beforeActionInvocation(Method actionMethod) {
Http.Request request = Http.Request.current();
if (request.controllerInstance == null) request.controllerInstance = getBeanOfType(request.controllerClass);
injectPlayPlugins();
}

private void loadInjector() {
Expand Down Expand Up @@ -94,7 +91,7 @@ private String moduleList() {
return moduleList.toString();
}

@Override
@Override
public <T> T getBeanOfType(Class<T> clazz) {
if (injector == null) {
return null;
Expand Down Expand Up @@ -130,6 +127,12 @@ private void injectAnnotated() {
}
}

private void injectPlayPlugins() {
for (PlayPlugin plugin : Play.pluginCollection.getAllPlugins()) {
injector.injectMembers(plugin);
}
}

private boolean isInjectable(Field field) {
return Modifier.isStatic(field.getModifiers()) &&
(field.isAnnotationPresent(javax.inject.Inject.class) || field.isAnnotationPresent(com.google.inject.Inject.class));
Expand Down
4 changes: 4 additions & 0 deletions src/play/modules/guice/InjectSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Deprecated
/**
* @deprecated Don't use this feature. Static fields are evil.
*/
public @interface InjectSupport {
}
24 changes: 5 additions & 19 deletions test/play/modules/guice/GuicePluginTest.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
package play.modules.guice;

import org.junit.Test;
import play.mvc.Controller;
import play.mvc.Http;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertNull;

public class GuicePluginTest {
@Test
public void createNonStaticControllerInstance() throws Exception {
Http.Request.current.set(new Http.Request());
NonStaticController controllerBean = new NonStaticController();
public void getBeanOfType_returnsNull_ifInjectorIsNull() {
GuicePlugin plugin = new GuicePlugin();
plugin.injector = null;

GuicePlugin plugin = new GuicePlugin() {
@Override public <T> T getBeanOfType(Class<T> clazz) {
return (T) controllerBean;
}
};
plugin.beforeActionInvocation(NonStaticController.class.getMethod("hello"));

assertThat(Http.Request.current().controllerInstance, is(controllerBean));
}

static class NonStaticController extends Controller {
public void hello() {}
assertNull(plugin.getBeanOfType(String.class));
}
}