Chris' Tech Blog

March 18, 2011

Setting static properties with Spring

Filed under: Java, Spring — Chris Ey @ 10:58 am

Sometimes I need to set a static property with Spring, and each time the Google result seems a bit hard to comb through.

Partly probably because Spring guys think static property injection is evil, but sometimes it just makes sense!

So for my benefit (and yours too) here is a link to a pretty nice tutorial about this subject:

http://planproof-fool.blogspot.com/2010/03/spring-setting-static-fields.html

And if you don’t like the non-static setter to a static property (like me), here is the code to directly inject a static property for your applicationContext.xml:

    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod" value="de.inweb.blog.BadDesign.setTheProperty"/>
        <property name="arguments">
            <list>
                <ref bean="theProperty"/>
            </list>
       </property>
    </bean>

August 21, 2010

Hibernate Event Listeners – Mappings for Spring

Filed under: Hibernate, Java, Spring — Chris Ey @ 1:15 am

When I wanted to use hibernate event listeners or interceptors to modify an entity before it is saved and after it is loaded from the DB, I did not find a satisfying list of available events! Fortunately I found this post (in German) which simply listed all hibernate mappings from the code:

http://www.nautsch.net/2008/05/16/hibernate-eventlisteners-in-spring-konfigurieren/

I think – as I too didn’t find a better documentation about it – this is a great idea.

And it proves once more: Code is the best documentation. :)

static {
                eventInterfaceFromType = new HashMap();

                eventInterfaceFromType.put("auto-flush", AutoFlushEventListener.class);
                eventInterfaceFromType.put("merge", MergeEventListener.class);
                eventInterfaceFromType.put("create", PersistEventListener.class);
                eventInterfaceFromType.put("create-onflush", PersistEventListener.class);
                eventInterfaceFromType.put("delete", DeleteEventListener.class);
                eventInterfaceFromType.put("dirty-check", DirtyCheckEventListener.class);
                eventInterfaceFromType.put("evict", EvictEventListener.class);
                eventInterfaceFromType.put("flush", FlushEventListener.class);
                eventInterfaceFromType.put("flush-entity", FlushEntityEventListener.class);
                eventInterfaceFromType.put("load", LoadEventListener.class);
                eventInterfaceFromType.put("load-collection", InitializeCollectionEventListener.class);
                eventInterfaceFromType.put("lock", LockEventListener.class);
                eventInterfaceFromType.put("refresh", RefreshEventListener.class);
                eventInterfaceFromType.put("replicate", ReplicateEventListener.class);
                eventInterfaceFromType.put("save-update", SaveOrUpdateEventListener.class);
                eventInterfaceFromType.put("save", SaveOrUpdateEventListener.class);
                eventInterfaceFromType.put("update", SaveOrUpdateEventListener.class);
                eventInterfaceFromType.put("pre-load", PreLoadEventListener.class);
                eventInterfaceFromType.put("pre-update", PreUpdateEventListener.class);
                eventInterfaceFromType.put("pre-delete", PreDeleteEventListener.class);
                eventInterfaceFromType.put("pre-insert", PreInsertEventListener.class);
                eventInterfaceFromType.put("pre-collection-recreate", PreCollectionRecreateEventListener.class);
                eventInterfaceFromType.put("pre-collection-remove", PreCollectionRemoveEventListener.class);
                eventInterfaceFromType.put("pre-collection-update", PreCollectionUpdateEventListener.class);
                eventInterfaceFromType.put("post-load", PostLoadEventListener.class);
                eventInterfaceFromType.put("post-update", PostUpdateEventListener.class);
                eventInterfaceFromType.put("post-delete", PostDeleteEventListener.class);
                eventInterfaceFromType.put("post-insert", PostInsertEventListener.class);
                eventInterfaceFromType.put("post-commit-update", PostUpdateEventListener.class);
                eventInterfaceFromType.put("post-commit-delete", PostDeleteEventListener.class);
                eventInterfaceFromType.put("post-commit-insert", PostInsertEventListener.class);
                eventInterfaceFromType.put("post-collection-recreate", PostCollectionRecreateEventListener.class);
                eventInterfaceFromType.put("post-collection-remove", PostCollectionRemoveEventListener.class);
                eventInterfaceFromType.put("post-collection-update", PostCollectionUpdateEventListener.class);
                eventInterfaceFromType = Collections.unmodifiableMap( eventInterfaceFromType );
}

And this is how you can use this information:

In Spring, you can configure an event listener for a session factory:

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

        [...]

        <property name="eventListeners">
            <map>
                <entry key="save-update">
                    <ref local="yourSaveOrUpdateListener" />
                </entry>
                <entry key="save">
                    <ref local="yourSaveOrUpdateListener" />
                </entry>
                <entry key="update">
                    <ref local="yourSaveOrUpdateListener" />
                </entry>
                <entry key="post-load">
                    <ref local="yourPostLoadListener" />
                </entry>

                [...]

            </map>
        </property>

        [...]

    </bean>

You can use the key in Hibernate’s map above to know which events are available and how to name the key in the spring configuration. The class name above tells you which interface to implement for the given listener.

Powered by WordPress