Announcement Announcement Module
Collapse
No announcement yet.
Inject EntityManager in a custom PropertyPlaceHolderConfigurer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Inject EntityManager in a custom PropertyPlaceHolderConfigurer

    Hello,

    I would like to implements a custom PropertyPlaceholderConfigurer to load my properties from a database.

    So I just add a Bean like this :

    Code:
    @Repository
    @Transactional
    public class DatabasePropertyPlaceholderConfigurerImpl extends PropertyPlaceholderConfigurer {
    
        @PersistenceContext
        private EntityManager em;
    
        public DatabasePropertyPlaceholderConfigurerImpl() {
            super();
            setPlaceholderPrefix("${");
        }
    
        @Override
        protected void loadProperties(final Properties props) throws IOException {
           // DO STUFF HERE WITH ENTITYMANAGER TO LOAD THE PROPS
        }
    }
    The problem is that the entitymanager is null !

    The configuration is the same for my DAO's and it works!

    I've just noticed that if I remove "extends PropertyPlaceholderConfigurer", the entitymanager is well injected.

    Can someone tell me why it doesn't work in a PropertyPlaceholderConfigurer?

    Thank you.

    Stéphane

  • #2
    I can't say why it doesn't work. But I'm wondering if you could do this using a combination of lazy-init="true" and depends-on="entityManagerFactory" in the bean definitions which require values from the database. You may not even need the two together -- I don't have time today to mock it up to say for sure.

    (I'm assuming that in order to configure your entity manager, you are using properties from .properties files or the environment, otherwise it seems that you'd have a chicken-and-egg situation where you need the entity manager to load the properties needed to initialize it.)

    Here's a StackOverflow question which seems to be very similar to what you are trying to do. It uses Java configuration, but I'm guessing you could do the same thing in XML.

    Comment


    • #3
      Hello,

      Thanks for reply!

      I think the problem is that @PersistenceContext annotation is processed after the PropertyPlaceholderConfigurer are created.

      As a work around, I set my class as implementing BeanFactoryAware and keep the beanFactory as a local attribute. Then a get the EntityManagerFactory with the "getBean" method :

      Code:
      @Component
      public class DatabasePropertyPlaceholderConfigurerImpl extends PropertyPlaceholderConfigurer implements BeanFactoryAware {
      
          private BeanFactory beanFactory;
      
          @SuppressWarnings("unchecked")
          @Override
          protected void loadProperties(final Properties props) throws IOException {
              EntityManagerFactory emf = beanFactory.getBean(EntityManagerFactory.class);
              EntityManager em = emf.createEntityManager();
              
              Query query = em.createQuery("select p from ApplicationParameter p");
              List<ApplicationParameter> resultList = query.getResultList();
              for (ApplicationParameter p : resultList) {
                  props.put(p.getName(), p.getValue());
              }
          }
      
          /**
           * @see org.springframework.beans.factory.config.PlaceholderConfigurerSupport#setBeanFactory(org.springframework.beans.factory.BeanFactory)
           */
          @Override
          public void setBeanFactory(BeanFactory beanFactory) {
              super.setBeanFactory(beanFactory);
              this.beanFactory = beanFactory;
          }
      }

      Comment

      Working...
      X