You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@deltaspike.apache.org by rickqinj <ri...@aliyun.com> on 2016/07/20 07:13:43 UTC

Restrictions of using BeanProvider?

Hello,The BeanProvider makes me confused for a few days. Could anyone help me out?The problem in my case is that I cannot get an instance of a non-managed class by BeanProvider.getContextualReference() method.I listed my case as following:
// DeltaSpike 1.4.2, Weld 1.1.31, CDI 1.0-SP4
//--Class A--public class A {    //member fields
    private B b;    //Constructor with parameters.    public A(String p1, String p2, String p3) {        //...just do something.
    }    public B getInstanceOfB() {        b = BeanProvider.getContextualReference(B.class, false);        return b;
    }
}
//--Class Caller--@Named
public class Caller {    //...    public void action() {        A a = new A("s1", "s2", "s3");        B b = a.getInstanceOfB(); //<-------- I cannot get an instance of B.    }    //...
}
The class A is a non-managed bean, and Caller is managed.
Am I do wrong with BeanProvider? What should I do to get the instance of B?Ps. I cannot make the class A managed, it may cause refactoring of all project. (~that's awful~)And according to the error log, it seems that a missing InjectionPoint may caused this problem.
Many thanks for your help!



Re: Restrictions of using BeanProvider?

Posted by rickqinj <ri...@aliyun.com>.
Hello Thomas,Many thanks! It works. 
------------------------------------------------------------------From:Thomas Andraschko <an...@gmail.com>Time:2016 Jul 20 (Wed) 16:34To:DeltaSpike Maillist - Users <us...@deltaspike.apache.org>; rickqinj <ri...@aliyun.com>Subject:Re: Restrictions of using BeanProvider?
Hi,

BeanProvider just works for managed beans as it just "provide beans" from the underlying CDI container.

If you would like to do injection in a unmanaged object, you can do the following:

MyUnmanagedBean a = new MyUnmanagedBean();
BeanProvider.injectFields(a);

Regards,
Thomas


2016-07-20 9:13 GMT+02:00 rickqinj <ri...@aliyun.com>:
Hello,The BeanProvider makes me confused for a few days. Could anyone help me out?The problem in my case is that I cannot get an instance of a non-managed class by BeanProvider.getContextualReference() method.I listed my case as following:

// DeltaSpike 1.4.2, Weld 1.1.31, CDI 1.0-SP4

//--Class A--public class A {    //member fields

    private B b;    //Constructor with parameters.    public A(String p1, String p2, String p3) {        //...just do something.

    }    public B getInstanceOfB() {        b = BeanProvider.getContextualReference(B.class, false);        return b;

    }

}

//--Class Caller--@Named

public class Caller {    //...    public void action() {        A a = new A("s1", "s2", "s3");        B b = a.getInstanceOfB(); //<-------- I cannot get an instance of B.    }    //...

}

The class A is a non-managed bean, and Caller is managed.

Am I do wrong with BeanProvider? What should I do to get the instance of B?Ps. I cannot make the class A managed, it may cause refactoring of all project. (~that's awful~)And according to the error log, it seems that a missing InjectionPoint may caused this problem.

Many thanks for your help!





Re: Restrictions of using BeanProvider?

Posted by Thomas Andraschko <an...@gmail.com>.
Hi,

BeanProvider just works for managed beans as it just "provide beans" from
the underlying CDI container.

If you would like to do injection in a unmanaged object, you can do the
following:

MyUnmanagedBean a = new MyUnmanagedBean();
BeanProvider.injectFields(a);

Regards,
Thomas


2016-07-20 9:13 GMT+02:00 rickqinj <ri...@aliyun.com>:

> Hello,The BeanProvider makes me confused for a few days. Could anyone help
> me out?The problem in my case is that I cannot get an instance of a
> non-managed class by BeanProvider.getContextualReference() method.I listed
> my case as following:
> // DeltaSpike 1.4.2, Weld 1.1.31, CDI 1.0-SP4
> //--Class A--public class A {    //member fields
>     private B b;    //Constructor with parameters.    public A(String p1,
> String p2, String p3) {        //...just do something.
>     }    public B getInstanceOfB() {        b =
> BeanProvider.getContextualReference(B.class, false);        return b;
>     }
> }
> //--Class Caller--@Named
> public class Caller {    //...    public void action() {        A a = new
> A("s1", "s2", "s3");        B b = a.getInstanceOfB(); //<-------- I cannot
> get an instance of B.    }    //...
> }
> The class A is a non-managed bean, and Caller is managed.
> Am I do wrong with BeanProvider? What should I do to get the instance of
> B?Ps. I cannot make the class A managed, it may cause refactoring of all
> project. (~that's awful~)And according to the error log, it seems that a
> missing InjectionPoint may caused this problem.
> Many thanks for your help!
>
>
>