DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Dependency Injection with Play Framework and Google Guice

Dependency Injection with Play Framework and Google Guice

Felipe Oliveira user avatar by
Felipe Oliveira
·
Jun. 08, 11 · Interview
Like (0)
Save
Tweet
Share
15.31K Views

Join the DZone community and get the full member experience.

Join For Free
[img_assist|nid=41705|title=|desc=|link=none|align=left|width=80|height=100]

Guice is a lightweight dependency injection framework developed by Google and it's a good alternative if you are looking for DI on your Play Framework-based application. Keep in mind that the framework is lightweight but you still need to evaluate your application specifically and decide what should be managed by Guice and what should not. It does add some overhead in terms of development time which is the plumming that Play! provides and we grew to love.


It's very common for applications using DI, especially Spring-based ones, to overuse the DI framework by loading every bean/class imaginable into the context. By the way I don't think this issue has anything to do with Spring itself, just developers mis-using the framework and Spring happens to be the most popular Java-based DI framework so the issue becomes more noticeable. That's a sure recipe for a very heavy context, with a slow startup and all that jazz we should desperately try to avoid. I have just recently ran into an application loading a StringUtils type of class into a Spring context, I still don't understand why. If you know certain parts of your application don't need separate or different behaviors based on a certain context, you probably don't need Dependency Injection.

Enough talk let's get down to the implementation.

1) First add the Guice module to your application (dependencies.yml if you are using Play 1.2 or higher)
require:
    - play
    - play -> guice 1.2


2) Then define your module
public class MyModule extends AbstractModule {

	@Override
	protected void configure() {
		this.bind(MyBean.class).toProvider(ReallyCoolBean.class).in(Singleton.class);
	}

}

3) Then create your Injector
public class ApplicationDependencies extends GuiceSupport {

	/**
	 * Guice Application Injector
	 *
	 * @see play.modules.guice.GuiceSupport#configure()
	 */
	@Override
	protected Injector configure() {
		return Guice.createInjector(new MyModule());
	}
}

4) Now use @Inject wherever you want
@InjectSupport
public class MyService {

	@Inject
	static MyBean myBean;

}
And Voila! Please visit Guice's documentation to learn more about the framework but this is what you need to do to use it on your Play Framework application.

I have just found out my ex-boss David Baker who is currently a Google employee has done some work on the framework which I thought was very cool. I have just met him here in New York after 12 years, thank you David for showing me around, I had a great time!

And by the way checkout DI with Scala and the Cake Pattern, very cool! That's a link to Jonas Boner's blog, Mr. Akka, which is also a great framework but I am leaving that for another day.

Originally posted at http://geeks.aretotally.in/dependency-injection-with-play-framework-and-google-guice.
Framework Dependency injection Play Framework Google Guice Google (verb) application

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Create a CLI Chatbot With the ChatGPT API and Node.js
  • When Should We Move to Microservices?
  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • Choosing the Right Framework for Your Project

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: