After several years of development in Spring, I switched to EJB, and I'm not happy that I have no solution for this use case. Let's say this is a strategy template implemented on a map. In Spring, it might look like this.
<bean id="myBean" class="MyBeanImpl"> <property name="handlers"> <map> <entry key="foo" value-ref="fooHandler"/> <entry key="bar" value-ref="barHandler"/> </property> </bean>
In EJB / CDI, I have this.
@Stateless public class MyBeanImpl implements MyBean { private Map<String, Class<? extends Handler>> handlers = new HashMap<>(); @PostConstruct public void init() { handlers.put("foo", FooHandlerImpl.class); handlers.put("bar", BarHandlerImpl.class); }
Remember that jndi search works with implementations, not interfaces. Isn't there a better solution? And no, I donβt want to have separate fields (foo, bar), enter them and create a map afterwards (this can be a huge list and change often). Ideally, in the case of a configuration change, I would not touch the MyBeanImpl class at all.
banterCZ
source share