I have an issue with duplicating many beans across several projects and wanted to reduce this by creating 1 repo that produces the beans. So far I’m struggling. Forgive me if this has already been answered, I’m still learning the terminology related to CDI so may be searching for the wrong answers.
This is what I’ve tried:
In the project I want to create a bean I have:
@Dependent
public class MyBean {
@Produces
public SomeBean someBean(){
return new SomeBean();
}
}
Then I created a META-INF/beans.xml file under resources.
I package as a <type>jar</type>
, mvn clean install
and everything is fine.
Now I import this bean into another project like so:
@ApplicationScoped
public class MyBean {
private final SomeBean someBean;
public MyBean(SomeBean someBean) {
this.someBean = someBean;
}
public void callBean(){
try {
someBean.doSomething()
}catch(Exception e) {
e.printStackTrace()
}
}
}
The issue then arises when I attempt to test this bean:
@QuarkusTest
public class BeanTest {
@Inject
MyBean myBean;
@Test
void testMyBean() {
myBean.callBean();
}
}
This results in a unsatisfied dependency for type MyBean, so obviously I’ve not indexed the bean correctly.
Where am I going wrong? Should I even be sharing beans across projects or is there a better way to tackle this issue?
Holly maine is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.