The CDI specification adds a new feature to use CDI into a code that works outside the CDI container.
Though this feature does not use those CDI can still use the callback (@PostConstruct and @PreDestroy) and injection with @Inject record within its class. Keep in mind that these annotations are not part of the CDI specifications but of @Inject.
For example, this class included in a non-CDI archive will work anyway:
public class Car {
@Inject
SomeClass someInstance;
@PostConstruct
public void init() {
...
}
@Predestroy
public void cleanUp() {
...
}
}
Here's how we can obtain an instance of an object through Unmanaged:
Unmanaged<Car> unmanaged = new Unmanaged(Car.class);
UnmanagedInstance<Car> unmanagedCar = unmanaged.newInstance();
Car car = unmanagedCar.produce().inject().postConstruct().get();