public interface Cdi
IoC pattern. The annotations from JSR 250 and JSR 330 are combined to configure component
implementations. A CDI framework can understand these standardized annotations and
assembles your components performing the dependency injection. We strongly recommend to use spring as IocContainer but there are also other
implementations like JBoss Seam, Google Guice (GIN for GWT)/Eclipse SiSu, etc.
public interface MyComponent {
void doSomething();
}
public interface OtherComponent {
void doSomethingElse();
}
//this annotation declares this as a component that is configured by the IocContainer...
@Named
public class MyComponentImpl implements MyComponent {
private OtherComponent otherComponent;
// this annotation defines a dependency to OtherComponent. The IocContainer verifies that there is exactly
// one component available that implements OtherComponent and that gets automatically injected here...
@Inject
public void setOtherComponent(OtherComponent otherComponent) {
this.otherComponent = otherComponent;
}
// this annotation defines an initializer method that is called after all dependencies have been injected (e.g.
// setOtherComponent must have been called before).
@PostConstruct
public void initialize() {
// ... setup the component here ...
}
// this annotation defines an destroy method that is called when the application is shut down...
@PostConstruct
public void dispose() {
// ... free resources, release thread-pool, etc. ...
}
public void doSomething() {
// do something and maybe then
this.otherComponent.doSomethingElse();
}
}
// and finally to complete the example...
@Named
public class OtherComponentImpl implements OtherComponent {
public void doSomethingElse() {
// here we do something else...
}
}
Even though there are different variants how to do it, we strongly recommend using public setter injection
as it is the best approach for testability, debugging and getting most helpful messages in case of an
error. Also we provide AbstractLoggableComponent that you can extend
from.| Modifier and Type | Field and Description |
|---|---|
static String |
CDI_NAME
|
static String |
GET_INSTANCE
We strongly believe in the
Ioc pattern and try to avoid the keyword static except for
constants (in combination with final). |
static final String CDI_NAME
name of a
component for CDI. Named
(MyComponentInterface.CDI_NAME). static final String GET_INSTANCE
Ioc pattern and try to avoid the keyword static except for
constants (in combination with final). Books like
Design Patterns. Elements of Reusable Object-Oriented Software. introduced various patterns like
Singleton (or maybe AbstractFactory) that make use of static access and lead
to inflexible and bad design. Instead Ioc is the pattern of choice to get access to singletons or
factories. However, the problem is that Java does NOT support Ioc in its core. So for a small and
simple application it might be over-complicated to use it. Ioc design which allows extension and customization.getInstance() methods for convenience.StringUtil it is fine to
use StringUtilImpl.getInstance(). However if you want to make use of
many components of this project and decide for Ioc you can get all utilities and other components
injected. This gives you the final freedom to replace or extend components
without patching our code. *.gwt.xml to replace the
default implementation with a derived custom class extending the default and overriding methods to add
changed behavior. Copyright © 2001–2015 mmm-Team. All rights reserved.