通过Spring将字段注入由Hibernate加载的实体 [英] Injecting fields via Spring into entities loaded by Hibernate

查看:103
本文介绍了通过Spring将字段注入由Hibernate加载的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种通过Spring注入某些属性的方法,该bean通过Hibernate从DB加载。

I am looking for a way to inject certain properties via Spring in a bean that is loaded from the DB by Hibernate.

例如

class Student {
   int id; //loaded from DB
   String name; //loaded from DB
   int injectedProperty; //Inject via Spring
}

我可以配置Spring,以便每当Hibernate创建对象类学生,某些applicationContext文件中定义的某些属性注入对象创建?

Can I configure Spring so that whenever Hibernate creates objects of class Student, some properties as defined in some applicationContext file are injected with the object creation?

推荐答案

有一个设施,使用 AspectJ类织造与 @Configurable 注释。这将自动连接具有Spring依赖关系的注释类的任何新的实例,包括通过使用Hibernate的反射实例化的对象。

There is a facility for this, using AspectJ class weaving with the @Configurable annotation. This will auto-wire any new instance of an annotated class with Spring dependencies, including objects instantiated via reflection using the likes of Hibernate.

它需要一些类加载魔法,因此取决于与您的服务器平台的兼容性

It does require some class-loading magic, and so depends on compatibility with your server platform.

这篇关于通过Spring将字段注入由Hibernate加载的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆