java - How to pass parameter to injected class from another class in CDI? -
i new cdi, tried find solution question, but, couln't found any. question suppose have 1 class being injected(a) from, value(topass) getting injected, want pass same value(topass) class b, getting injected class a.
public class { string topass = "abcd"; // value not hardcoded @inject private b b; } public class b { private string topass; public b(string topass) { topass = topass; } } can please me in this? note: cannot initialize topass variable of b in same way have initialized in a, there restriction it. in spring have done easily, but, wanted in cdi.
you have options:
1. set topass variable b @postconstruct method of bean a:
@postconstruct public void init() { b.settopass(topass); } or
2. create producer topass variable , inject bean a , b.
producer:
@produces @topass public string producetopass() { ... return topass; } injection:
@inject @topass string topass; or
3. if bean a not dependent scoped bean can use provider interface obtain instance of bean a:
public class b { @inject provider<a> a; public void dosomeactionwithtopass() { string topass = a.get().gettopass()); ... } but should not use topass constructor or @postconstruct method.
Comments
Post a Comment