While the aspectj way works, I'd say the standard spring / hibernate way is to register a LoadEventListener (read more in the hibernate core reference, the spring reference and this thread)
here is a snip from the spring sessionfactory bean definition
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
...
<property name="eventListeners">
<map>
<entry key="post-load">
<bean class="com.foo.spring.MyLoadListener"></bean>
</entry>
</map>
</property>
</bean>
and here is the LoadEventListener:
public class MyLoadListener implements LoadEventListener{
public void setSpringManagedProperty(String springManagedProperty){
this.springManagedProperty = springManagedProperty;
}
private String springManagedProperty;
@Override
public void onLoad(LoadEvent event, LoadType loadType) throws HibernateException{
if(MyEntity.class.getName().equals(event.getEntityClassName())){
MyEntity entity = (MyEntity) event.getInstanceToLoad();
entity.setMyCustomProperty(springManagedProperty);
}
}
}
Look mom, no aspectj needed.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…