Announcement Announcement Module
Collapse
No announcement yet.
BeanPostProcessor with @Bean annotation not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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?

  • #2
    THe Bean(Factory)PostProcessor should be declared as a static bean (static method) not as a normal method.

    Comment


    • #3
      @Martin, Thank you for your time

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

      Comment


      • #4
        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, 03:26 PM.

        Comment


        • #5
          @Marten

          Thank you and Happy New Year

          Comment

          Working...
          X