To register a bean using annotation you need to annotate it with @Bean
annotation and pass values for following properties:
name
- name under which item should be registeredactive
-true
if bean should be enabled without enabling it in the configuration (however it is still possible to disable it using configuration)parent
- class of the parent bean which activation should trigger registration of your bean. In most cases parent class should be implementingRegistrarBean
parents
- array of classes which should be threaten asparent
classes if more than one parent class is required (optional)exportable
-true
if bean should be visible in all descendant kernels (in other case default visibility rules will be applied) (optional)selectors
- array of selector classes which will decide whether class should be registered or not (optional)
Tip
If parent
is set to Kernel.class
it tells kernel to register this bean in the root/main kernel (top-level kernel).
If you want your bean SomeDependencyBean
to be registered when another bean ParentBean
is being registered (like a required dependency), you may annotate your bean SomeDependencyBean
with @Bean
annotation like this example:
@Bean(name = "nameOfSomeDependencyBean", parent = ParentBean.class, active = true) public class SomeDependencyBean { ... }
Warning
Works only if bean registered as defaultBeanConfigurator
supports this feature. By default Tigase XMPP Server uses DSLBeanConfigurator
which is subclass of AbstractBeanConfigurator
which provides support for this feature.
If parent
is set to the class which is not implementing RegistrarBean
interface, then your bean will be registered in the same kernel scope in which parent bean is registered. If you do so, ie.
by setting parent to the class of the bean which is registered in the kernel1
and your bean will be also registered in kernel1
.
As the result it will be exposed to other beans in the same kernel scope. This also means that if you will configure it in
the same way as you would set parent
to the parent
of annotation of the class to which your parent
point to.
Example.
@Bean(name="bean1", parent=Kernel.class) public class Bean1 { @ConfigField(desc="Description") private int field1 = 0; .... } @Bean(name="bean2", parent=Bean1.class) public class Bean2 { @ConfigField(desc="Description") private int field2 = 0; .... }
In this case it means that bean1
is registered in the root/main kernel instance. At the same time, bean2
is also registered to the root/main kernel as its value of parent
property of annotation points to class not implementing RegistrarBean
.
To configure value of field1
in instance of bean1
and field2
in instance of bean2
in DSL (for more information about DSL format please check section DSL file format
of the Admin Guide
) you would need to use following entry in the config file:
bean1 { field1 = 1 } bean2 { field2 = 2 }
As you can see, this resulted in the bean2
configuration being on the same level as bean1
configuration.