CDI: manually creating beans with constructor arguments

I have a class that I want to bean

public class SomeBean{ public SomeBean(){ //default constructor } public SomeBean(String someStr){ //constructor with arguments. } } 

To create a CDI bean manually, I do the following

 Bean<?> bean = (Bean<?>) beanManager.resolve(beanManager.getBeans(SomeBean.class)); SomeBean someBean =(SomeBean) beanManager.getReference(bean, bean.getBeanClass(), beanManager.createCreationalContext(bean)); 

However, the above method will create an instance of SomeBean wth by default. How can I create a bean and pass a String argument to construcot? Postscript CDI - WELD

+5
source share
1 answer

The standard way to define beans with given constructor arguments is through a producer method , for example

 @Produces @ApplicationScoped @MyQualifier public SomeBean myBean() { return new SomeBean("foo"); } 

Application code should usually not use BeanManager unless you want to create a CDI extension.

0
source

All Articles