Language: Java
Dependency Injection
Guice was created by Google to simplify dependency management in Java applications. Instead of manually instantiating objects and passing dependencies, Guice allows developers to define how objects are wired together using modules and annotations like @Inject. It supports scopes, providers, and AOP features, making it suitable for both small and enterprise applications.
Google Guice is a lightweight dependency injection framework for Java. It allows you to manage object creation and dependencies declaratively using annotations, reducing boilerplate and promoting loose coupling.
Add dependency in pom.xml:
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>5.1.0</version>
</dependency>Add dependency in build.gradle:
implementation 'com.google.inject:guice:5.1.0'Guice provides a declarative way to bind interfaces to implementations and inject dependencies. It supports constructor, method, and field injection, scopes (singleton, request, etc.), providers, and AOP interceptors.
import com.google.inject.*;
interface Service {
void execute();
}
class ServiceImpl implements Service {
public void execute() { System.out.println("Service executed"); }
}
class Client {
private final Service service;
@Inject
Client(Service service) { this.service = service; }
void doWork() { service.execute(); }
}
public class Main {
public static void main(String[] args) {
Injector injector = Guice.createInjector(binder -> binder.bind(Service.class).to(ServiceImpl.class));
Client client = injector.getInstance(Client.class);
client.doWork();
}
}Binds the Service interface to ServiceImpl and injects it into Client automatically.
class Client {
@Inject private Service service;
void doWork() { service.execute(); }
}Demonstrates injecting dependencies directly into fields.
class Client {
private final Provider<Service> serviceProvider;
@Inject
Client(Provider<Service> serviceProvider) { this.serviceProvider = serviceProvider; }
void doWork() { serviceProvider.get().execute(); }
}Uses Provider to lazily fetch instances of a dependency when needed.
binder.bind(Service.class).to(ServiceImpl.class).in(Singleton.class);Ensures a single instance of ServiceImpl is shared across the application.
class AppModule extends AbstractModule {
@Override
protected void configure() {
bind(Service.class).to(ServiceImpl.class);
}
}
Injector injector = Guice.createInjector(new AppModule());Organizes bindings in a module for cleaner configuration.
class Client {
private Service service;
@Inject void setService(Service service) { this.service = service; }
}Injects dependencies using a setter method.
Prefer constructor injection over field or method injection for better testability.
Use Modules to centralize and manage bindings cleanly.
Leverage Singleton scope for stateless or shared services to save resources.
Use Providers for lazy or conditional dependency creation.
Combine Guice with AOP for cross-cutting concerns like logging and transactions.