Announcement Announcement Module
Collapse
No announcement yet.
BeanFactoryPostProcessor disable @Autowired annotation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • BeanFactoryPostProcessor disable @Autowired annotation

    Hi,

    I try to implement a BeanFactoryPostProcessor that generate BeanDefinition beans in the Spring Context, and to do that I need to get spring beans wiche are already reference by annotation like @component.

    So I write this code in my BeanFactoryPostProcessor :
    ...
    beanFactory.getBean("myfirstbean");
    ...

    But if I do this, all attributs of "myfirstbean" annotate with @Autowired are null and so I have error like NullPointerException...
    I don't understand the link between the call of beanFactory.getBean() in the BeanFactoryPostProcessor and the execution of AutowiredAnnotationBeanPostProcessor!

    I create a very simple maven project to reproduce the problem with a Junit test, it can be download here : http://www.partage-fichiers.com/upload/8kqa15fd
    Utilisateur : gblp98g3
    Mot de passe : v8ir2k7q

    also the code is:
    @Component(value="myfirstbean")
    public class MyFirstBean {

    @Autowired
    public MySecondBean secondBean;

    public MyFirstBean() {}
    }
    @Component(value="mysecondBean")
    public class MySecondBean {

    public MySecondBean() {}

    }
    public class RemotingBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {
    beanFactory.getBean("myfirstbean");
    }

    }
    <beans ...>

    <context:component-scan base-package="com.test" />

    <bean id="mypostProcessor" class="com.test.RemotingBeanFactoryPostProcessor" />

    </beans>
    Thank you to help me.

  • #2
    There is no bean at the moment the BeanFactoryPostProcessor is called...

    BeanFactoryPostProcessors operate on BeanDefinitions and are as such executed BEFORE any instance of a bean is created... Your call to the getbean method simply gives a an instance but which isn't postprocessed...

    Basically BeanFactoryPostProcessors operate on the recipe on how to create a bean, BeanPostProcesors operate on bean instances.

    Comment


    • #3
      So if I undestand, a BeanFactoryPostProcessors can create a beanDefinition in the context Spring only if this beanDefinition not reference an other bean (defined in XML spring config file).

      For example, if we have:

      <beans ...>
      <bean id="myBean" class="com.test.MyBean" />
      </beans>
      The creation of this BeanDefinition in a BeanFactoryPostProcessors is right:

      ...
      BeanDefinition definition = new RootBeanDefinition("com.test.MyNewBean");
      definition.getPropertyValues().add("param1", "param1 value" );
      definition.getPropertyValues().add("param2", "param2 value");
      ...

      The creation of this other BeanDefinition in a BeanFactoryPostProcessors can't work:

      ...
      BeanDefinition definition = new RootBeanDefinition("com.test.MyNewBean");
      definition.getPropertyValues().add("param1", beanFactory.getBean("myBean"));
      definition.getPropertyValues().add("param2", "param2 value");
      ...

      So, there is a solution to create a BeanDefinition that do reference to other Spring Bean?
      I think about create the BeanDefinition with a BeanPostProcessors with some String attributes, and create the link to the real Spring Bean in a BeanPostProcesors , but it's seems to be a little complicated...Do you have better solution?

      Comment


      • #4
        No it isn't... If you want to reference a bean simply reference the bean definition of it, you don't need the actual instance.. Remember you are still operating on bean definitions (i.e. the recipe).

        Take a look at the BeanDefinitionParser instances which enable the tx:annotation-driven for instance, they also create BeanDefinition instances.
        Last edited by Marten Deinum; Apr 6th, 2011, 07:53 AM.

        Comment


        • #5
          I find a solution with a child and a parent Spring context. I suppose the child is created after the parent, so the beans are all postprocessed.

          But I'm going to see how BeanDefinitionParser works, it's could be a better way.
          Thank's a lot for the quality of your answer.

          Comment

          Working...
          X