Results 1 to 5 of 5

Thread: BeanPostProcessor with @Bean annotation not working

  1. #1
    Join Date
    Oct 2007
    Location
    Bangalore
    Posts
    29

    Default BeanPostProcessor with @Bean annotation not working

    I'm trying to create a `BeanPostProcessor` for registering some values to a Map.

    The `BeanPostProcessor` works file if I'm create the bean instance via xml definition, but I change the bean definition to `@Configuration` class it is not working.

    PostProcessor
    Code:
    public class InstantiationTracingBeanPostProcessor implements BeanPostProcessor {
        
          public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
              return bean;
          }
    
          public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
              System.out.println("Bean '" + beanName );
              return bean;
          }
        }
    Bean Configuration
    Code:
        import org.springframework.beans.factory.annotation.Qualifier;
        import org.springframework.context.annotation.Bean;
    
        @org.springframework.context.annotation.Configuration
        public class Configuration {
            @Bean
            public @Qualifier("InstantiationTracingBeanPostProcessor")
            InstantiationTracingBeanPostProcessor activitiConfigurationBeanPostProcessor() {
                return new InstantiationTracingBeanPostProcessor();
            }
        }
    Component scan Configuration
    Code:
    <context:component-scan base-package="xyz.config"/>
        <context:annotation-config/>
    The application just hangs if I use the above configuration. But if I use xml based configuration as given below it works fine.

    Code:
    <bean class="xyz.bean.InstantiationTracingBeanPostProcessor"/>
    I'm using spring 3.1.0.
    What am I doing wrong here?

    With Regards,
    Arun P Johny

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    THe Bean(Factory)PostProcessor should be declared as a static bean (static method) not as a normal method.
    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
    Join Date
    Oct 2007
    Location
    Bangalore
    Posts
    29

    Default

    @Martin, Thank you for your time

    Can you what you mean by static bean? The documentation does not say anything about this.

    With Regards,
    Arun P Johny

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Thought that was pretty clear.

    THe Bean(Factory)PostProcessor should be declared as a static bean (static method) not as a normal method.
    Code:
    @Configuration
    public class Configuration {
      @Bean 
      public static InstantiationTracingBeanPostProcessor activitiConfigurationBeanPostProcessor() {
        return new InstantiationTracingBeanPostProcessor();
      }
    }
    There is a difference in processing, basically a bean without static is treated as a FactoryBean which takes part in the container/bean lifecycle. Whereas static methods aren't treated as factory beans (at least not in the sense as a factory bean normally would).
    Last edited by Marten Deinum; Dec 26th, 2012 at 03:26 PM.
    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

  5. #5
    Join Date
    Oct 2007
    Location
    Bangalore
    Posts
    29

    Default

    @Marten

    Thank you and Happy New Year

    With Regards,
    Arun P Johny

Tags for this Thread

Posting Permissions

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