虽然aspectj方式有效,但我认为标准的弹簧/休眠方法是注册LoadEventListener(在休眠核心参考,弹簧参考和此线程中阅读更多内容)
这是从春季会议工厂豆的定义中摘录的
<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>
这里是 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);
}
}
}
看妈妈,不需要任何方面j。