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

more documentation, give example for api #2

Open
tkruse opened this issue Apr 3, 2017 · 12 comments
Open

more documentation, give example for api #2

tkruse opened this issue Apr 3, 2017 · 12 comments

Comments

@tkruse
Copy link

tkruse commented Apr 3, 2017

Hi,
nice project, but I struggled to get it to work with my play 2.5 spring 4.3 project.

Maybe you could add an example application config and Spring Configuration, like:

# Required as workaround, see spring-application-loader docs
play.bindings.disabled += "play.libs.Crypto"
play.bindings.disabled += "play.api.libs.Crypto"
play.spring.configs = ["com.example.PlaySpringDIConfiguration"]

and

package com.example;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class PlaySpringDIConfiguration {

}
@tkruse
Copy link
Author

tkruse commented Apr 3, 2017

Things I struggle with:

  • Startup requires controllers.Default, solved this way:
    @Bean
    public Default playDefaultController() {
        // Need to provide this with Spring DI for Controllers.
        return new Default();
    }
  • How to run test using Plays WithApplication in Java.
    This misses bean controllers.Assets somehow (I see them mentioned in SpringApplicationLoader.defaultOverrieds, how can I use that for tests?):
public class ApplicationTest extends WithApplication {

   @Override
    protected Application provideApplication() {
        final play.api.Application scalaApp = new SpringApplicationBuilder()
                .build();
        return new Application() {
            @Override
            public play.api.Application getWrappedApplication() {
                return scalaApp;
            }

            @Override
            public Configuration configuration() {
                return new Configuration(scalaApp.configuration());
            }

            @Override
            public Injector injector() {
                return new DelegateInjector(scalaApp.injector());
            }
        };
    }
}

I can work around by providing an instance of controllers.Assets in my Spring configuration, leading me to the next problems:

  • How can I run tests with special Spring Configuration? How can I set active Spring Profiles? Basically: How can I access the Spring Context of a SpringApplication to do whatever pleases me with Spring?

@remithieblin
Copy link
Owner

Hi tkruse,
Thanks for the feedback!
I've updated the readme with your suggestions.

Regarding the tests, I''m not too sure, I'm not actively maintaining that project.
But i remember trying to keep the same "structure" as the Guice Application loader, so my best advice would be to look at similar tests based on Guice.
Maybe you did that already? And things are different?
Also possible that it's a java vs scala issue, as i only "tested" with scala projects.

I don't have time to actively maintain, but contribution are more than welcome! :)
Hope this helps a little,
Thanks again!

@tkruse
Copy link
Author

tkruse commented Apr 6, 2017

Hi @remithieblin,
thanks for the answer and the additional docs.
I am new to Play in general, so I struggle a lot with things that might be easy for others.

There is a GuiceApplicationBuilder for Java, which has bindings() and disable() methods to modify bindings from application.conf.
The SpringApplicationBuilder has no such methods. I tried using the Scala classes to add Configuration, but I get stuck for any methods that has a Scala immutable map as argument, as I cannot create those from Java8.

Overall I am not sure what is possible, most limitations come from Play2 itself I think, and their design decision to always load application.conf unless the JVM was started with special parameters.

@tkruse tkruse closed this as completed Apr 6, 2017
@tkruse tkruse reopened this Apr 6, 2017
@tkruse
Copy link
Author

tkruse commented Apr 10, 2017

As another idea: It would be nice if SpringBuilder would read a config property spring.profiles.active to set ctx.getEnvironment().setActiveProfiles(...)

Without that, projects will have to find other more hacky ways to set the Spring profile, such as always having to pass -Dspring.profiles.active=... to the JVM, as well as -Dconfig.resource=... for play

@paraniraja
Copy link

paraniraja commented Apr 11, 2017

@tkruse Thanks for the sample ApplicationTest, have tried using the same, beans are getting created but in services or in controller, Autowire binding is not working variables are null.

Any thoughts on this.

Thanks for sharing.

[Update] Using SpringJUnit4ClassRunner, application object is created, how to inject application context present in Play Application to Test Context.

@paraniraja
Copy link

@tkruse I have overridden spring testContext, now test cases are working with SpringJunit4Runner.

@paraniraja
Copy link

@tkruse How you have resolved "controllers.Assets" error while running test case.

@tkruse
Copy link
Author

tkruse commented Apr 15, 2017

Hi,
I believe I added something to my application.conf for controllers.Assets, let me check again at work...

@tkruse
Copy link
Author

tkruse commented Apr 18, 2017

Hm, for the time being I found no better solution than this:

    import controllers.Assets;
    import play.api.http.HttpErrorHandler;
    // ...

    @Configuration
    public class PlaySpringDIConfiguration

        @Bean
        // Required for tests only, Enable with Spring TEST profile only
        @Profile("TEST")
        public Assets playAssets(HttpErrorHandler handler) {
            return new Assets(handler);
        }

@tkruse
Copy link
Author

tkruse commented Apr 18, 2017

However, that's where I got stuck, I could not nicely set spring.profiles.active within the test. Only passing this on the command line worked, I think...
How did you "overridde spring testContext"?

@paraniraja
Copy link

Spring provide "BootstrapWith" annotation and then created couple of super class.
a) Play Context which extends DefaultTestContext --> Override applicationContext method
b) Context Manger which extends DefaultTestContextBootstrapper and then use above context

Regarding assets, I have to bind Assets in the application module.
Hope this helps.

@tkruse
Copy link
Author

tkruse commented Apr 19, 2017

Any chance you can share the super classes and a test example? I can try reproducing the same using your description, but an example would be faster

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants