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

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

问题描述

我正在寻找一种通过 Spring 在 Hibernate 从数据库加载的 bean 中注入某些属性的方法.

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 创建类 Student 的对象时,某些 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?

推荐答案

有一个工具,使用 使用 @Configurable 注释编织的 AspectJ 类.这将使用 Spring 依赖项自动连接带注释的类的任何 new 实例,包括使用 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天全站免登陆