Announcement Announcement Module
Collapse
No announcement yet.
Programmatically registering a bean. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Programmatically registering a bean.

    Hi,

    trying to execute this piece of code:

    Code:
    ClasspthXmlApplicationContext ctx;
    IBean bean;
    ((SingletonBeanRegistry) ctx).registerSingleton(IBean.class.getName(), bean);
    ctx.getBean(IBean.class) /* Throws NoSuchBeanException */
    Can anyone tell me how to fix this? Thanks!

  • #2
    Correction: The exception isn't thrown when doing ctx.getBean(), but when injecting a value into
    Code:
    @Inject IBean bean;

    Comment


    • #3
      Answering my own question: Had to change

      Code:
      ((SingletonBeanRegistry) ctx).registerSingleton(IBean.class.getName(), bean);
      to

      Code:
      ((SingletonBeanRegistry) ctx.getBeanFactory()).registerSingleton(IBean.class.getName(), bean);

      Comment

      Working...
      X