Announcement Announcement Module
Collapse
No announcement yet.
Question re: Writing a Custom Namespace and BeanPostProcessor (registration problem) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Question re: Writing a Custom Namespace and BeanPostProcessor (registration problem)

    Hi,

    I am currently writing a custom namespace and I am using a BeanPostProcessor to call a start method on the bean (the bean is of type Session). This start method returns true if the bean has, in fact, been started, and I intend to throw an exception if this ever returns false (hence the reason I'm not using a simple init method).

    There are a few issues I'm facing with this and some assistance would be appreciated.

    At the moment, I'm adding the bean definition for the BeanPostProcessor when the SessionBeanDefinitionParser's parseInternal method is invoked. I can see a log message indicating the BeanPostProcessor has been registered, however I do not see any log message (later) indicating the BeanPostProcessor's postProcessAfterInitialization method is ever invoked.

    The BeanPostProcessor is being registered as follows:

    beanDefinitionRegistry.registerBeanDefinition(bean PostProcessorId, beanDefinition);

    Can you tell me what I'm missing here? According to what I've read the container should detect that this is bean implements BeanPostProcessor and treat it as a special case.

    Finally, I'd rather just read this bean in from an xml file via the ApplicationContext just once, when the namespace is initialised, however I don't see an intuitive way of doing this. Is this possible, or is this an approach I shouldn't (or can't) pursue?

    Thanks for your help,

    Tom

  • #2
    OK so Eclipse generated a method stub, as follows, when I implemented the BeanPostProcessor:

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
    throws BeansException {
    // TODO Auto-generated method stub
    return null;
    }

    Keep in mind that this will cause a null value to be returned when a call is made to the container requesting a bean that has been passed through this BeanPostProcessor. I didn't spot this right away and as a result spent quite a bit of time wondering what I was doing wrong when, in fact, the BeanPostProcessor was being called.

    The method should look as follows:

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
    throws BeansException {
    return bean;
    }

    There is a nice example here (http://www.box-spring.org/boxspring-...ionParser.html) for adding a BeanPostProcessor:

    private void createPostProcessor(Element element, ParserContext parserContext) {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
    builder.getRawBeanDefinition().setBeanClass(Pathed DynamicMBeanPostProcessor.class);
    builder.addConstructorArgReference(EXPORTER_BEAN_N AME);
    builder.addConstructorArgValue(getId(element, parserContext.getRegistry()));
    AbstractBeanDefinition processorDef = builder.getBeanDefinition();
    String beanName = parserContext.getReaderContext().generateBeanName( processorDef);
    BeanDefinitionHolder holder = new BeanDefinitionHolder(processorDef, beanName);
    BeanDefinitionReaderUtils.registerBeanDefinition(h older, parserContext.getRegistry());
    }

    Comment

    Working...
    X