The document discusses dependency injection (DI) with programmatic configuration. It defines DI as a mechanism where dependencies between objects are not directly created but instead satisfied by a third party - a DI container. The document outlines reasons for using DI including loose coupling and inversion of control. It provides examples of DI in Java using annotations and explains concepts such as scopes, producers and programmatic configuration. It also discusses integrating DI into Vaadin applications using CDI extensions.
4. Dependency Injection (DI) is a runtime mechanism
where dependency between the client object and the
dependent object does not occur directly.
5. Dependency Injection (DI) is a runtime mechanism
where dependency between the client object and the
dependent object does not occur directly.
6. Dependency Injection (DI) is a runtime mechanism
where dependency between the client object and the
dependent object does not occur directly.
With DI the client object does not necessarily manage
the lifecycle of the dependent object.
7. Dependency Injection (DI) is a runtime mechanism
where dependency between the client object and the
dependent object does not occur directly.
With DI the client object does not necessarily
manage the lifecycle of the dependent object.
8. Dependency Injection (DI) is a runtime mechanism
where dependency between the client object and the
dependent object does not occur directly.
With DI the client object does not necessarily
manage the lifecycle of the dependent object.
Instead with DI a special DI container takes care of
the object lifecycle management
9. Dependency Injection (DI) is a runtime mechanism
where dependency between the client object and the
dependent object does not occur directly.
With DI the client object does not necessarily
manage the lifecycle of the dependent object.
Instead with DI a special DI container takes care of
the object lifecycle management
10. Dependency Injection (DI) is a runtime mechanism
where dependency between the client object and the
dependent object does not occur directly.
With DI the client object does not necessarily
manage the lifecycle of the dependent object.
Instead with DI a special DI container takes care of
the object lifecycle management where clients
reference managed and possibly shared objects.
11. Dependency Injection (DI) is a runtime mechanism
where dependency between the client object and the
dependent object does not occur directly.
With DI the client object does not necessarily
manage the lifecycle of the dependent object.
Instead with DI a special DI container takes care of
the object lifecycle management where clients
reference managed and possibly shared objects.
37. @Named("Audi")
public class AudiService implements VehicleService {
@Override
public void performService(Vehicle vehicle) {
System.out.println("Performing Audi service");
}
}
38. @Named("BMW")
public class BMWService implements VehicleService {
@Override
public void performService(Vehicle vehicle) {
System.out.println("Performing BMW service");
}
}
39. @Named("VW")
public class VWService implements VehicleService {
@Override
public void performService(Vehicle vehicle) {
System.out.println("Performing VW service");
}
}
67. public class VehicleOperater {
@Inject
@Any
private Instance<VehicleService> serviceInstantiator;
public void operate(Vehicle vehicle) {
NamedLiteral named = new NamedLiteral(vehicle.getMake());
}
}
68. public class VehicleOperater {
@Inject
@Any
private Instance<VehicleService> serviceInstantiator;
public void operate(Vehicle vehicle) {
NamedLiteral named = new NamedLiteral(vehicle.getMake());
VehicleService service = serviceInstantiator.select(named).get();
}
}
121. Lessons learned
1. Depend on abstractions through Dependency Inversion
2. Beans need to be managed by (CDI) container
122. Lessons learned
1. Depend on abstractions through Dependency Inversion
2. Beans need to be managed by (CDI) container
3. Inject to Fields, Methods and Constructor
123. Lessons learned
1. Depend on abstractions through Dependency Inversion
2. Beans need to be managed by (CDI) container
3. Inject to Fields, Methods and Constructor
4. Producer method allows programmatic bean creation and non-default
constructor
124. Lessons learned
1. Depend on abstractions through Dependency Inversion
2. Beans need to be managed by (CDI) container
3. Inject to Fields, Methods and Constructor
4. Producer method allows programmatic bean creation and non-default
constructor
5. Instance<T> provides on-demand instantiation with candidate selection
125. Lessons learned
1. Depend on abstractions through Dependency Inversion
2. Beans need to be managed by (CDI) container
3. Inject to Fields, Methods and Constructor
4. Producer method allows programmatic bean creation and non-default
constructor
5. Instance<T> provides on-demand instantiation with candidate selection
6. Use CDIUI and CDIView for making core Vaadin concepts managed beans
126. I would NOT start a single new
Vaadin project without
Dependency
Injection!