Results 1 to 5 of 5

Thread: Inject dependencies into objects controlled by Hibernate

  1. #1

    Default Inject dependencies into objects controlled by Hibernate

    Hi,

    i was wondering how to inject dependencies into objects retrieved from the DB by Hibernate.

    Any comment is appreciated.

    Ronald

  2. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    I remember reading a thread in this forum about this topic. I think the discussion was about using interceptors to proxy the hibernate objects. The proxies could then be parameterized with additional dependencies.
    Maybe you can try to find the particular thread to find out more.

    Regards,
    Andreas

  3. #3
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Here is what I do:

    Code:
      <!-- Hibernate Session Factory -->
      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation"><value>classpath:XXXXXX-hibernate.cfg.xml</value></property>
        <property name="eventListeners">
          <map>
            <entry key="post-load">
              <bean class="uk.mycompany.ClassFilteringLoadEventListener">
                <constructor-arg index="0">
                  <map>
                    <entry key="uk.mycompany.ClassIWantToAdvise">
                      <list>
                        <bean class="uk.mycompany.ClassThatDoesTheAdvisingLoadedListener">
                          <constructor-arg index="0" ref="someCollaborator"/>
                        </bean>
                      </list>
                    </entry>
                  </map>
                </constructor-arg>
              </bean>
            </entry>
          </map>
        </property>
        <property name="mappingDirectoryLocations" value="WEB-INF/classes"/>
      </bean>
    Let me talk you through that

    Basically I am specifiying a list of eventListeners that hibernate will call after loading the object. The first one (uk.mycompany.ClassFilteringLoadEventListener) is a predicate that will check the name of the class being loaded.

    The second (uk.mycompany.ClassThatDoesTheAdvisingLoadedListen er) is the class that actually manipulates the loaded object.

    Code:

    Code:
    public final class ClassFilteringLoadEventListener implements PostLoadEventListener {
        public static interface LoadedObjectListener {
            void loaded(final Object loadedObject);
        }
        private static final long serialVersionUID = 8652156835267250103L;
        private final Map<Class, Collection<LoadedObjectListener>> listeners;
    
        public ClassFilteringLoadEventListener(final Map<String, Collection<LoadedObjectListener>> theListeners) {
            // spring assumes the key is string, not class :(
            this.listeners = new HashMap<Class, Collection<LoadedObjectListener>>();
            for (Map.Entry<String, Collection<LoadedObjectListener>> entry: theListeners.entrySet()) {
                try {
                    Class clazz = Class.forName(entry.getKey());
                    listeners.put(clazz, entry.getValue());
                } catch (final ClassNotFoundException e) {
                    throw new BeanInitializationException("Cannot locate class " + entry.getKey(), e);
                }
            }
        }
    
        public void onPostLoad(final PostLoadEvent event) {
            Object loadedObject = event.getEntity();
            informListeners(loadedObject, listeners);
        }
    
        public static void informListeners(final Object loadedObject, final Map<Class, Collection<LoadedObjectListener>> listeners) {
            for (Map.Entry<Class, Collection<LoadedObjectListener>> entry: listeners.entrySet()) {
                if (loadedObject.getClass().isAssignableFrom(entry.getKey())) {
                    for (LoadedObjectListener listener: entry.getValue()) {
                        listener.loaded(loadedObject);
                    }
                }
            }
        }
    }
    This accepts a list of LoadedObjectListener to pass the loaded object to (if the class matches).

    Is it the most elegant, probably not

    At the moment I have specific implementations of LoadedObjectListener, but you could easily create an implementation which gets hold of the appropriate BeanFactory and performs the autowiring on the loaded object.

    HTH.

  4. #4

    Default Thanks for the info

    Hi Andreas and Yatesco,

    thanks for your time and the info. I will look into this.

    Best,
    Ronald

  5. #5
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Modified the code a bit for anyone who is interested:

    http://blogs.warwick.ac.uk/colinyate...ate_spring_di/

Posting Permissions

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