Announcement Announcement Module
Collapse
No announcement yet.
Problems Autowiring in PropertyPlaceholderConfigurer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems Autowiring in PropertyPlaceholderConfigurer

    Hello. I have an interesting problem that I was hoping somebody could help me with:


    Situation:
    I've built a custom configuration API on top of Apache Commons Configuration that allows me to detect which environment the application is deployed in. To keep this short, my API reads in an 'environment' value from the JNDI tree and then uses this value to load in configuration files specific to that environment.

    The API itself uses the Spring IoC container and has been unit tested within and without successfully.

    My goal is to be able to use my environment aware configuration API as an extension to the PropertyPlaceholderConfigurer so that I can configure properties within my spring xml config.

    Problem:
    I've created my own custom 'EnvironmentAwarePropertyPlaceholderConfigurer' and inside this class I'm autowiring my configuration object. The issue is that it's not autowiring successfully; it's actually null.

    Is there a reason why beans wouldn't be autowired successfully to an instance of the PropertyPlaceholderConfigurer?

    Is the IoC container not intialized at this point in the life-cycle?

  • #2
    While searching for a solution, it definitely appears that there's a priority order for bean initialization and instantiation within the IoC container (which makes sense). In my specific case, it appears that the PropertyPlaceholderConfigurer is instantiated at the top of the chain so when I try to autowire in my Configuration class, the Configuration class and all it's dependencies have yet to be resolved, initialized and/or instantiated.

    Does anyone know if there's a way to set the order in which dependencies get resolve/initialized/instantiated? Or, is there another way that I can manually fetch my Configuration class and all it's dependencies as some sort of 'init' step in my custom PropertyPlaceholderConfigurer?

    Comment


    • #3
      Originally posted by leafknode View Post
      Is there a reason why beans wouldn't be autowired successfully to an instance of the PropertyPlaceholderConfigurer?

      Is the IoC container not intialized at this point in the life-cycle?
      If you think about it, the PropertyPlaceholderConfigurer is used to set the properties of beans before they have been instantiated, so the IoC container can't be fully initialized at that point. PropertyPlaceholderConfigurer is actually a BeanFactoryPostProcessor. The placeholders are substituted at a relatively early stage, by definition before other beans have been instantiated, so autowiring it is probably not a good idea.

      Have you tried using plain setter or constructor injection?

      Comment


      • #4
        Yeah, I've tried both Constructor and setter injection, neither of which work.

        I even tried to create my Configuration object manually by overriding the 'postProcessBeanFactory(ConfigurableListableBeanFa ctory beanFactory)' and calling 'createBean' for my Configuration object but it still won't resolve my Configuration dependency tree. It returns an instance but all of it's deps are nulled out.

        Comment


        • #5
          Finally figured out a hacky way to accomplish what I needed. In the example below, Bar depends on Foo and in order to successfully autowire it, I had to override postProcessBeanFactory() and then use the beanFactory to register deps.

          public void postProcessBeanFactory(ConfigurableListableBeanFac tory beanFactory) throws BeansException {

          Foo foo = beanFactory.getBean(Foo.class);
          beanFactory.autowireBeanProperties(foo, ConfigurableListableBeanFactory.AUTOWIRE_BY_TYPE, true);
          beanFactory.registerResolvableDependency(Foo.class , foo);

          Bar bar = (Bar) beanFactory.autowire(Bar.class, ConfigurableListableBeanFactory.AUTOWIRE_CONSTRUCT OR, true);

          ....

          }

          Comment

          Working...
          X