Indicates that an annotated class or interface is a bean. Inside a module, a bean represents one or more instances that can be wired to other bean instances visible to this module.
A bean is fully identified by its name (which defaults to the name of the class) and the name of the module exposing the bean (eg. [MODULE_NAME]:[BEAN_NAME]). We can differentiate three kinds of beans: module bean, wrapper bean and socket bean.
A module bean is automatically instantiated and wired. Its dependencies must
be defined in injection points or sockets which can be either the constructor
for required dependencies or setter methods for optional dependencies. By
convention, any setter method is considered as a socket which may lead to
ambiguities. In that case a @BeanSocket annotation can be
used to specify explicit bean sockets.
@Bean
public class ModuleBean implements SomeService {
public ModuleBean(RequiredDependency requiredDependency) {
...
}
public void setOptionalDependency(OptionalDependency optionalDependency) {
...
}
@Init
public void init() {
...
}
@Destroy
public void destroy() {
...
}
}
A wrapper bean is used to expose legacy code that can't be instrumented. A
wrapper bean must be a class annotated with @Bean and
@Wrapper and implements Supplier.
@Bean
@Wrapper
public class WrapperBean implements Supplier<SomeService> {
private WeakReference<SomeService> instance;
public WrapperBean(RequiredDependency requiredDependency) {
// Instantiate the wrapped instance
this.instance = new WeakReference<>(...)
}
public void setOptionalDependency(OptionalDependency optionalDependency) {
// Set optional dependency on the instance
this.instance.set...
}
public SomeService get() {
return this.instance.get();
}
@Init
public void init() {
// Init the instance
this.instance.get().init();
}
@Destroy
public void destroy() {
// Destroy the instance
this.instance.get().destroy();
}
}
A socket bean is a particular type of bean which is used to declare a module
dependency that is a bean required or desirable by the beans in the module to
operate properly. As for bean socket, it should be seen as an injection point
at module level to inject an external bean into the module (hence the
"socket" designation). From a dependency injection perspective, inside the
module, a socket bean is considered just like any other bean and is
automatically or explicitly injected in beans visible to the module. A socket
bean must be an interface annotated with @Bean with a
Bean.Visibility.PUBLIC visibility and extends Supplier.
@Bean
public interface SocketBean implements Supplier<SomeService> {
}
- Since:
- 1.0
- Author:
- Jeremy Kuhn
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumIndicates the strategy to use to instantiate the bean.static enumIndicates the visibility of a bean in a module. -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionIndicates a name identifying the bean in the module, defaults to the name of the class.The bean strategy which defaults toBean.Strategy.SINGLETON.Indicates the visibility of the bean in the module.
-
Element Details
-
name
String nameIndicates a name identifying the bean in the module, defaults to the name of the class.
- Returns:
- A name
- Default:
- ""
-
visibility
Bean.Visibility visibilityIndicates the visibility of the bean in the module.
Usually, you're most likely to create public beans exposed to other modules. Private bean are provided as a convenience to let the framework instantiate and wire internal beans instead of doing it explicitly.
- Returns:
- The bean's visibility
- Default:
- PUBLIC
-
strategy
Bean.Strategy strategyThe bean strategy which defaults toBean.Strategy.SINGLETON.- Returns:
- The bean's strategy
- Default:
- SINGLETON
-