JBoss.org Community Documentation

11.4. Injecting context information

You may recall that inside the microcontainer each bean is represented by a context which holds amongst other things the bean's current state. This context information can be injected into bean properties or constructor/factory-method parameters using the @Inject annotation or <inject> element together with a fromContext attribute.


@Inject(bean="otherBean",
        fromContext="beanInfo")
public void setBeanInfo(BeanInfo beanInfo) {
    ...
}

<bean name="sndBean" class="org.jboss.test.NameAwareBean">
    <property name="beaninfo">
        <inject bean="otherBean" fromContext="beaninfo"/>
    </property>
</bean>

If you want to inject information about the current bean's context then you can simply omit the bean attribute.


@Inject(fromContext="beanInfo")
public void setBeanInfo(BeanInfo beanInfo) {
    ...
}

<bean name="sndBean" class="org.jboss.test.NameAwareBean">
    <property name="beaninfo">
        <inject fromContext="beaninfo"/>
    </property>
</bean>

The fromContext attribute can take the following values:

  • name - the name of the bean

  • aliases - a list of the bean's other names

  • metadata - information about the bean from annotations or deployment descriptors

  • beaninfo - information about the bean from the bean class

  • scope - the microcontainer scope that the bean belongs to

  • id - the internal id of the bean within the microcontainer

  • context - the bean context

Important

All context information is wrapped into unmodifiable objects to prevent the user from changing anything outside of the microcontainer's control.