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

  • Testing a BeanPostProcessor

    I coded an example from the Just Spring book which implements a BeanPostProcessor. The example uses an xml configuration file which declares one bean and the BeanPostProcessing bean. My understanding is the BeanPostProcessing bean will be detected automatically by Spring and applied to all the other beans in the container. This appears to work as advertised with one slight catch.. I test the bean with the following code:

    public static void main( String[] args) {

    GenericXmlApplicationContext ctx = null;

    // Create container with xml file
    ctx = new GenericXmlApplicationContext("classpath:bean-post-proc-config.xml");
    // ctx.refresh();

    // Obtain a Trade object from the container
    Trade trade = (Trade)ctx.getBean("trade");

    .....

    Where Trade is the one bean declared in the xml file. The catch is that when I uncomment out the ctx.refresh() I get an error about calling refresh() twice:

    Exception in thread "main" java.lang.IllegalStateException: GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once
    at org.springframework.context.support.GenericApplica tionContext.refreshBeanFactory(GenericApplicationC ontext.java:242)
    at org.springframework.context.support.AbstractApplic ationContext.obtainFreshBeanFactory(AbstractApplic ationContext.java:467)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:397)
    at com.justspring.ch3.beanpostproc.BeanPostProcExampl e.main(BeanPostProcExample.java:17)

    Can someone expain this error?

  • #2
    Please use [ code][/code ] tags when posting code/xml/stacktraces, that way it remains readable.

    The stacktrace tells you why. The applicationcontext is automatically initialized meaning that refresh is called, from the constructor. When you call it it runs again which, as the stacktrace tells you, isn't allowed on the GenericXmlApplicationContext.

    Comment


    • #3
      BeanPostProcessor

      Originally posted by Marten Deinum View Post
      Please use [ code][/code ] tags when posting code/xml/stacktraces, that way it remains readable.

      The stacktrace tells you why. The applicationcontext is automatically initialized meaning that refresh is called, from the constructor. When you call it it runs again which, as the stacktrace tells you, isn't allowed on the GenericXmlApplicationContext.
      Yes, I looked at the documentation. It does not address any situations where refresh is called automatically on the GenericXmlApplicationContext or ApplicationContext that I could find.

      Comment

      Working...
      X