Results 1 to 3 of 3

Thread: Testing a BeanPostProcessor

  1. #1

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default BeanPostProcessor

    Quote 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.

Posting Permissions

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