[jboss-user] [Microcontainer] - Re: Injecting a field value
alesj
do-not-reply at jboss.com
Tue Dec 2 12:29:06 EST 2008
"david.lloyd at jboss.com" wrote : The most obvious mechanism to me is a ValueMetaData that is comprised of a class and a field name.
The most obvious is value-factory:
| <bean name="ConstantsProvider" class="org.jboss.demos.ioc.access.ConstantsProvider">
| <constructor>
| <parameter>org.jboss.demos.ioc.access.IConstants</parameter>
| </constructor>
| </bean>
|
| <bean name="XConstUser2" class="org.jboss.demos.ioc.access.XConstUser">
| <property name="x"><value-factory bean="ConstantsProvider" method="getConstant" parameter="X_INT_VALUE"/></property>
| </bean>
|
| public class ConstantsProvider
| {
| private KernelConfigurator configurator;
| private String className;
| private ClassInfo classInfo;
|
| public ConstantsProvider(String className)
| {
| this.className = className;
| }
|
| @Inject(bean = KernelConstants.KERNEL_CONFIGURATOR_NAME)
| public void setConfigurator(KernelConfigurator configurator)
| {
| this.configurator = configurator;
| }
|
| public void create() throws Throwable
| {
| if (configurator == null)
| throw new IllegalArgumentException("Null configurator");
|
| if (className != null)
| classInfo = configurator.getClassInfo(className, getClass().getClassLoader());
| }
|
| public Object getConstant(String constantName) throws Throwable
| {
| return getConstant(classInfo, constantName);
| }
|
| public Object getConstant(String className, String constantName) throws Throwable
| {
| ClassInfo classInfo = configurator.getClassInfo(className, getClass().getClassLoader());
| return getConstant(classInfo, constantName);
| }
|
| protected Object getConstant(ClassInfo classInfo, String constantName) throws Throwable
| {
| FieldInfo field = classInfo.getDeclaredField(constantName);
| return field.get(null);
| }
| }
|
I can add this as part of Kernel,
probably next to BeanMetaDataBuilder.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4193805#4193805
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4193805
More information about the jboss-user
mailing list