You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by zeddius <av...@gmail.com> on 2012/11/21 07:04:09 UTC

How to access a bean private field during injection?

Hi guys,

This question is more generic than TomEE related however I decided to ask
here because you TomEE devs might have a better internal look on this.

What I'm trying to do is to inject a primitive value into a bean field from
some configuration file but leaving it's default value if config is not
found.

/@Inject @Config("someValue")
private Integer someValue = 4;/

I have a producer looking like this
/public @Produces @Config Integer getIntProperty(InjectionPoint p) {
  ...
  if (p.getMember() instanceof Field) {
    Field injectionField = (Field) p.getMember();
    injectionField.setAccessible(true);
        	
    CreationalContext<?> cc =
beanManager.createCreationalContext(p.getBean());
    Object target = beanManager.getReference(p.getBean(),
p.getBean().getBeanClass(), cc);
			
    try {
      // I want first fetch a default value and then return either a config
value if it's not null or default value
      Object defaultValue = injectionField.get(target);
    } catch (Exception e) {}
}/

The issue here is that the field value on the bean is always null. I suspect
this is because a bean is a proxy and there is a delegate instance
somewhere.
So my question what is the proper way of accessing the bean private fields?
I've been looking at portable extenstions and eventually found some example
that might help but it seems a bit overhead to me. Is there an easy way of
doing this?



--
View this message in context: http://openejb.979440.n4.nabble.com/How-to-access-a-bean-private-field-during-injection-tp4658743.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: How to access a bean private field during injection?

Posted by zeddius <av...@gmail.com>.
For those who interested there is a dirty workaround:

In the bean you have:
/@Inject @Config("someValue") 
private Integer someValue = 4;/

and producer like this:
/public @Produces @Config Integer getIntProperty(InjectionPoint p) { 
  ... 
  if (p.getMember() instanceof Field) { 
    Field injectionField = (Field) p.getMember(); 
    Class<?> beanClass = injectionField.getDeclaringClass();

    try {
       Object beanInstance = beanClass.newInstance();
       injectionField .setAccessible(true);
       return (Integer) injectionField .get(beanInstance);
    } catch (Exception e) {
       return null;
    }
   
}/

Another way is to use:
/@PostConstruct
public void init() {
   if (someValue == null) {
       someValue = DEFAULT_SOME_VALUE;
   }
}/



--
View this message in context: http://openejb.979440.n4.nabble.com/How-to-access-a-bean-private-field-during-injection-tp4658743p4658818.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: How to access a bean private field during injection?

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Think the lifecycle doesnt (at least in some case) allow it, the value
should be set in annotation or sthg else
Le 22 nov. 2012 07:10, "zeddius" <av...@gmail.com> a écrit :

> Hi Romain,
>
> /someValue /is set to 4 in declaration and has /@Config("someValue")/ which
> tells to take this value from the property named 'someValue'. If there is
> such a property the /someValue /will be set to the property value, but if
> there is no such property then null is returned by producer and /someValue
> /will be set to null. What I want is that it would remain 4 instead of
> /null/.
>
> I'll try to look at DeltaSpike meanwhile. Thank you.
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/How-to-access-a-bean-private-field-during-injection-tp4658743p4658786.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: How to access a bean private field during injection?

Posted by zeddius <av...@gmail.com>.
Hi Romain,

/someValue /is set to 4 in declaration and has /@Config("someValue")/ which
tells to take this value from the property named 'someValue'. If there is
such a property the /someValue /will be set to the property value, but if
there is no such property then null is returned by producer and /someValue
/will be set to null. What I want is that it would remain 4 instead of
/null/.

I'll try to look at DeltaSpike meanwhile. Thank you.



--
View this message in context: http://openejb.979440.n4.nabble.com/How-to-access-a-bean-private-field-during-injection-tp4658743p4658786.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: How to access a bean private field during injection?

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi,

i don't really get why you do it in the producer simply get annotation for
default value and return your Integer no?

BTW did you look deltaspike @ConfigProperty?

*Romain Manni-Bucau*
*Twitter: @rmannibucau <https://twitter.com/rmannibucau>*
*Blog: **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
*LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
*Github: https://github.com/rmannibucau*




2012/11/21 zeddius <av...@gmail.com>

> Hi guys,
>
> This question is more generic than TomEE related however I decided to ask
> here because you TomEE devs might have a better internal look on this.
>
> What I'm trying to do is to inject a primitive value into a bean field from
> some configuration file but leaving it's default value if config is not
> found.
>
> /@Inject @Config("someValue")
> private Integer someValue = 4;/
>
> I have a producer looking like this
> /public @Produces @Config Integer getIntProperty(InjectionPoint p) {
>   ...
>   if (p.getMember() instanceof Field) {
>     Field injectionField = (Field) p.getMember();
>     injectionField.setAccessible(true);
>
>     CreationalContext<?> cc =
> beanManager.createCreationalContext(p.getBean());
>     Object target = beanManager.getReference(p.getBean(),
> p.getBean().getBeanClass(), cc);
>
>     try {
>       // I want first fetch a default value and then return either a config
> value if it's not null or default value
>       Object defaultValue = injectionField.get(target);
>     } catch (Exception e) {}
> }/
>
> The issue here is that the field value on the bean is always null. I
> suspect
> this is because a bean is a proxy and there is a delegate instance
> somewhere.
> So my question what is the proper way of accessing the bean private fields?
> I've been looking at portable extenstions and eventually found some example
> that might help but it seems a bit overhead to me. Is there an easy way of
> doing this?
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/How-to-access-a-bean-private-field-during-injection-tp4658743.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>