Results 1 to 3 of 3

Thread: Programmatically registering a bean.

  1. #1
    Join Date
    Feb 2012
    Posts
    11

    Default 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. #2
    Join Date
    Feb 2012
    Posts
    11

    Default

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

  3. #3
    Join Date
    Feb 2012
    Posts
    11

    Default

    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);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •